2013-05-09 99 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 
     } 
} 

,如果你得到任何更多的問題,寫評論,我會回來給你:)

相關問題