65.9K
CodeProject 正在变化。 阅读更多。
Home

将二维数组绑定到 DataGrid

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.55/5 (38投票s)

2004年1月9日

1分钟阅读

viewsIcon

389361

downloadIcon

12888

将二维数组绑定到 DataGrid。

引言

数据绑定是指在运行时从包含数据的结构自动设置一个或多个表单控件的属性的过程。更有趣的是自定义对象的绑定;其中一种情况是将二维数组绑定到 DataGrid

这有点棘手,但总的来说,它包括实现一些代表数据视图的类,并且这些类需要实现一系列接口。

在这里,你可以看到库的接口和内部类结构。

Using the Code

这很简单。

//
// 1. Create two dimensional array
//
const int  dim = 1000;

double[,]  array = new double[dim,dim];

Random ran = new Random();
for(int r = 0; r < dim; r++)
{
    for(int c = 0; c < dim; c++)
    {
        array[r,c] = (ran.Next(dim)); // fill it with random numbers.
    }
}

// 2. Create ArrayDataView class in which 
// constructor you pass the array 
// and assign it to DataSource property of DataGrid. 

 dataGrid1.DataSource = new ArrayDataView(array);

当你在数组中更改数据并希望在 DataGrid 中看到更新时,你需要调用 ArrayDataView 类的 Reset 方法。你可以使用各种基本类型,如 doubleint32string 等。

此外,还有一个带有附加参数的构造函数,该参数是列名数组,长度必须等于数组的列数,否则会引发异常。

历史

  • 2004年1月7日:创建
  • 2004年1月29日:添加了库的源代码

许可证

本文没有明确的许可证附加到它,但可能包含在文章文本或下载文件本身中的使用条款。如有疑问,请通过下面的讨论区联系作者。作者可能使用的许可证列表可以在 此处 找到。

© . All rights reserved.