9
我將如何去在C#.Net 4.0中將2維整數數組顯示到DataGridView控件中?2維整數數組到DataGridView
我將如何去在C#.Net 4.0中將2維整數數組顯示到DataGridView控件中?2維整數數組到DataGridView
按照代碼示例此頁面上填充Rows
屬性:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx
編輯
原來,這比我想象的有點棘手。下面是一個代碼示例:
var data = new int[4,3]
{
{ 1, 2, 3, },
{ 4, 5, 6, },
{ 7, 8, 9, },
{ 10, 11, 12 },
};
var rowCount = data.GetLength(0);
var rowLength = data.GetLength(1);
for (int rowIndex = 0; rowIndex < rowCount; ++rowIndex)
{
var row = new DataGridViewRow();
for(int columnIndex = 0; columnIndex < rowLength; ++columnIndex)
{
row.Cells.Add(new DataGridViewTextBoxCell()
{
Value = data[rowIndex, columnIndex]
});
}
dataGridView1.Rows.Add(row);
}
得到Merlyn的解決方案正常工作,您需要設置列算你行添加到DataGridView前:
dataGridView1.ColumnCount = 3;
這應該是一個評論而不是解決方案 – boctulus 2016-03-01 00:14:43
這應該。當我寫答案(評論)的時候,我是新的stackoverflow。 – randoms 2016-03-01 06:40:44