【问题标题】:2-dimensional Integer array to DataGridView二维整数数组到 DataGridView
【发布时间】:2011-05-05 21:47:59
【问题描述】:

如何在 C# .Net 4.0 中将二维整数数组显示到 DataGridView 控件中?

【问题讨论】:

    标签: c# .net datagridview .net-3.5 .net-4.0


    【解决方案1】:

    要让 Merlyn 的解决方案发挥作用,您需要在向 datagridview 添加行之前设置列数:

    dataGridView1.ColumnCount = 3;
    

    【讨论】:

    • 应该是评论而不是解决方案
    • 应该的。在我写答案(评论)时,我是 stackoverflow 的新手。
    【解决方案2】:

    按照此页面上的代码示例填充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);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多