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

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

问题描述

这看起来可能是简单的问题的许多你,但希望我试图做的是我使用读取文本文件上的click事件的StreamReader (ASP .NET和放大器,读取该文本文件我正在分割每行,分隔符,然后我在存储DataTable的列的它的每个部分,然后结合后C#)数据表到我的GridView控件,我的问题是我已经写了code,但即时得到空的GridView这样

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

结果
GridView的列标题我从设计师创造了 =的AutoGenerateColumns假

我的code是

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.

推荐答案

如何是这样的:

    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天全站免登陆