二维整数数组转DataGridView [英] 2-dimensional Integer array to DataGridView

查看:26
本文介绍了二维整数数组转DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How would I go about displaying a 2-dimensional integer array into a DataGridView Control in C# .Net 4.0?

推荐答案

按照此页面上的代码示例来填充 Rows 属性:

Follow the code sample on this page to populate the Rows property:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx

编辑

事实证明这比我想象的要棘手一些.这是一个代码示例:

Turns out this is a bit thornier than I thought. Here's a code example:

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);
}

这篇关于二维整数数组转DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆