从url读取文本文件并在Gridview Asp.net中填充

时间:2017-07-04 09:27:55

标签: asp.net

我已经编写了来自url的阅读和文本文件,并在Gridview中填充。问题是textfile在第一行中被一个空格分割,但在第二行和第三行有很多行。如何在我的文本文件中填写下面的gridview和C#代码Txt文件包含这样的数据 strong text

*

  

强文

* 错误的ServiceName ProductName SubsCount SubsRevenue RenewalCount RenewalRevenue TotalRev
20170310 PayPerUse PayPerUse_ATN_News_VideoAlert 546 1110.81 0 0 1110.81 20170311 PayPerUse PayPerUse_ATN_News_VideoAlert 549 1063.23 0 0 1063.23

要读取和填充的C#代码是

Stream data = client.OpenRead("my URL text File ");
            DataTable table = new DataTable();
            table.Columns.Add("Misdate."); //Misdate ServiceName ProductName SubsCount   SubsRevenue RenewalCount    RenewalRevenue TotalRev
            table.Columns.Add("ServiceName.");
            table.Columns.Add("ProductName");
            table.Columns.Add("SubsCount");
            table.Columns.Add("SubsRevenue");
            table.Columns.Add("RenewalCount");
            table.Columns.Add("RenewalRevenue");
            table.Columns.Add("TotalRev");
            using (StreamReader reader = new StreamReader(data))

            //using (StreamReader reader = new StreamReader(@"E:\textfile\usss.txt"))
            {
                // string s = reader.ReadToEnd();

                while (!reader.EndOfStream)
                {
                    //  List<string> parts = reader.ReadLine().Split(',').ToList();
                    string[] parts = reader.ReadLine().Split(' ');
                    // Response.Write(parts);
                    // table.Rows.Add(parts.ToString());
                    //string s = parts[0];
                   table.Rows.Add(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7]);
                }

            }
            GridView1.DataSource = table;
            GridView1.DataBind();

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

var newRow = table.NewRow();
for (int k=0; k<table.Columns.Count(); k++)
   newRow[k] = parts[k];

table.Rows.add(newRow);

希望它会有所帮助