2013-05-09 52 views
0

我正在尝试以编程方式将数据添加到DataGridView,但它似乎不工作。 我所拥有的是一个数组,这是我从一个文本文件中填写:DataGridView:以编程方式在特定单元格上添加数据

public static string PathList = @"C:\Users\gbbb\Desktop\Pfade.txt"; 
_PathRows = System.IO.File.ReadAllLines(@PathList); 

,我有4个列上我,因为我有路径添加尽可能多的行,因此一个DataGridView:

public void InitPathsTable() 
{ 
TabelleBib.Rows.Add(_PathRows.Length); 
//And here is where i want to add the Paths on Column Nr.4 
} 

接下来我需要的是一种将我得到的所有路径(24)添加到列Nr.4, 每行一个路径。 但是,像我这样的初学者似乎几乎不可能,所以我问你。

+0

如何你想添加数据到第4列?你得到了什么(错误)? – Fabio 2013-05-09 18:37:39

回答

0

这是可以为你做到的方法。阅读评论(尤其是确保你已经添加4列到你DataGridView):

public void InitPathsTable() 
{ 
     int rowindex; 
     DataGridViewRow row; 

     foreach (var line in _PathRows) 
     { 
       rowindex = TabelleBib.Rows.Add(); //retrieve row index of newly added row 
       row = TabelleBib.Rows[rowindex]; //reference to new row 

       row.Cells[3].Value = line; //set value of 4th column to line. WARNING: TabelleBib has to have 4 columns added either from code or designer othwerwise here you will get exception 
     } 
} 

,如果你得到任何更多的问题,写评论,我会回来给你:)

相关问题