【发布时间】:2011-05-05 21:47:59
【问题描述】:
如何在 C# .Net 4.0 中将二维整数数组显示到 DataGridView 控件中?
【问题讨论】:
标签: c# .net datagridview .net-3.5 .net-4.0
如何在 C# .Net 4.0 中将二维整数数组显示到 DataGridView 控件中?
【问题讨论】:
标签: c# .net datagridview .net-3.5 .net-4.0
要让 Merlyn 的解决方案发挥作用,您需要在向 datagridview 添加行之前设置列数:
dataGridView1.ColumnCount = 3;
【讨论】:
按照此页面上的代码示例填充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);
}
【讨论】: