在运行时使用文本文件填充Gridview [英] Populate Gridview at runtime using textfile

查看:137
本文介绍了在运行时使用文本文件填充Gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于许多人来说,似乎是一个简单的问题,但是我想要做的是我正在使用 StreamReader (ASP)点击事件阅读文本文件.net& C#)读取该文本文件后,我用,分隔符分隔每一行,然后它的每一部分都存储在 Datatable的列中,然后绑定datatable到我的gridview,我的问题是我已经编写了代码,但是我得到这样的空gridview这样





Gridviews列标题我从设计器创建了 autogeneratecolumns =false / p>

我的代码是

  protected void readfile_Click(object sender,EventArgs e )
{
string line;
DataTable dt = new DataTable();
using(StreamReader sr = new StreamReader(@D:\Temp\fileread\readtext.txt))
{
while((line = sr.ReadLine()) != null)
{
string [] parts = line.Split(',');
dt.Rows.Add(); (int i = 0; i< parts.Length; i ++)
{
dt.Columns.Add();

dt.Rows [0] [i] = parts [i];
MyGridView.DataSource = dt;
MyGridView.DataBind();
}
}
sr.Close();
}

我的文本文件有数据

  1,1,4,2,#,描述1 
5,5,4,2,#,描述2
3,3 ,6,3,#,描述3
2,2,4,2,#,描述4
4,5,4,2,#,描述5

希望你得到我想要的东西。

解决方案

如何这样做:

  protected void readfile_Click(object sender,EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add(行号);
table.Columns.Add(Col No.);
table.Columns.Add(宽);
table.Columns.Add(Height);
table.Columns.Add(Image URL);
table.Columns.Add(Description);

using(StreamReader sr = new StreamReader(@D:\Temp\fileread\readtext.txt))
{
while(!sr.EndOfStream)
{
string [] parts = sr.ReadLine()。Split(',');
table.Rows.Add(parts [0],parts [1],parts [2],parts [3],parts [4],parts [5]);
}
}
MyGridView.DataSource = table;
MyGridView.DataBind();
}


it may seem to be simple problem to many of you, but want i am trying to do is i am reading a text file on a click event using StreamReader (ASP.net & C#) after reading that text file i am splitting each line with ',' delimiter and then each part of it i am storing in Datatable's column and then binding the datatable to my gridview, my problem is i have written the code but i m getting empty gridview like this


Gridviews Column header i have created from designer with autogeneratecolumns="false"

my code is

protected void readfile_Click(object sender, EventArgs e)
{
    string line;
    DataTable dt = new DataTable();
    using (StreamReader sr = new StreamReader(@"D:\Temp\fileread\readtext.txt"))
    {
        while ((line = sr.ReadLine()) != null)
        {
            string[] parts = line.Split(',');
            dt.Rows.Add();
            for (int i = 0; i < parts.Length; i++)
            {
                dt.Columns.Add();
                dt.Rows[0][i] = parts[i];
                MyGridView.DataSource = dt;
                MyGridView.DataBind();
            }
        }
        sr.Close();
    }

my text file has data

1,1,4,2,"#",Description1
5,5,4,2,"#",Description2
3,3,6,3,"#",Description3
2,2,4,2,"#",Description4
4,5,4,2,"#",Description5

Hope you got it what i am trying to ask.

解决方案

How about something like this:

    protected void readfile_Click(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("Row No.");
        table.Columns.Add("Col No.");
        table.Columns.Add("Width");
        table.Columns.Add("Height");
        table.Columns.Add("Image URL");
        table.Columns.Add("Description");

        using (StreamReader sr = new StreamReader(@"D:\Temp\fileread\readtext.txt"))
        {
            while (!sr.EndOfStream)
            {
                string[] parts = sr.ReadLine().Split(',');
                table.Rows.Add(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]);
            }
        }
        MyGridView.DataSource = table;
        MyGridView.DataBind();
    }

这篇关于在运行时使用文本文件填充Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆