我試圖使用自爆代碼從文本文件中的數據導入到網格視圖:導入格式的文本文件的GridView C#
DataTable dt = new DataTable();
using (System.IO.TextReader tr = File.OpenText((@"d:\\My File3.log")))
{
string line;
while ((line = tr.ReadLine()) != null)
{
string[] items = line.Trim().Split(' ');
if (dt.Columns.Count == 0)
{
// Create the data columns for the data table based on the number of items
// on the first line of the file
for (int i = 0; i < items.Length; i++)
dt.Columns.Add(new DataColumn("Column" + i, typeof(string)));
}
dt.Rows.Add(items);
}
//show it in gridview
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
我的文件是這樣的:
ABC
EFDCC
EDDD
DP
然後我收到以下錯誤
輸入數組比列數此表不再C#應用程序
你正在爲每一行添加列。你應該只添加一次。 –