Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#

Binding a Two Dimensional Array to a DataGrid

Rate me:
Please Sign up or sign in to vote.
3.55/5 (38 votes)
1 Feb 20041 min read 378.4K   12.8K   55   69
Binding a two dimensional array to a DataGrid
Binding of two dimensional arrays to DataGrid is little bit tricky, but in general, it consists of implementing some classes which represent view of the data and they need to implement ending list of interfaces. Here, you can see interfaces and internal class structure of the library.

Image 1

Introduction

Data binding refers to the process of automatically setting properties of one or more form controls at run time from a structure that contains data. More interesting point is binding of custom objects; one of these cases is binding of two dimensional arrays to DataGrid.

The thing is little bit tricky, but in general, it consists of implementing some classes which represent view of the data and they in order need to implement ending list of interfaces.

Image 2

Here, you can see interfaces and internal class structure of the library.

Using the Code

It is very simple.

C#
//
// 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);

When you change data in array and want to see update in DataGrid, you need to call Reset method of ArrayDataView class. You can use all kinds of base types like double, int32, string and so on.

In addition, there is a constructor with additional parameter which is array of column names, length must be equal to columns of the array, if not, exception is raised.

History

  • 7th January, 2004: Created
  • 29th January, 2004: Source code of library added

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.


Written By
United States United States
http://www.mommosoft.com

Comments and Discussions

 
AnswerRe: How to handle editing in DataGrid? Pin
Inkus16-Dec-05 0:38
Inkus16-Dec-05 0:38 
GeneralRow headers Pin
midgetgemmy11-Nov-05 0:57
midgetgemmy11-Nov-05 0:57 
GeneralRe: Row headers Pin
Mihail Stefanov13-Nov-05 10:49
Mihail Stefanov13-Nov-05 10:49 
Generalthanks a lot!! Pin
Member 218834815-Sep-05 9:02
Member 218834815-Sep-05 9:02 
Generalthank you Pin
glenn4203-Feb-05 10:10
glenn4203-Feb-05 10:10 
Generalcoverting to vb.net Pin
viet2k28-Jan-05 7:02
sussviet2k28-Jan-05 7:02 
GeneralRe: coverting to vb.net Pin
Mihail Stefanov29-Jan-05 9:52
Mihail Stefanov29-Jan-05 9:52 
GeneralArray with no rows Pin
ThoBla198318-Jan-05 21:57
ThoBla198318-Jan-05 21:57 
GeneralRe: Array with no rows Pin
Mihail Stefanov19-Jan-05 6:35
Mihail Stefanov19-Jan-05 6:35 
GeneralRe: Array with no rows Pin
ThoBla198319-Jan-05 22:27
ThoBla198319-Jan-05 22:27 
GeneralThis includes no source code Pin
Pål K Tønder20-Jan-04 12:26
Pål K Tønder20-Jan-04 12:26 
GeneralRe: This includes no source code Pin
wiebe228-Jan-04 11:08
wiebe228-Jan-04 11:08 
GeneralRe: This includes no source code Pin
Anonymous28-Jan-04 11:43
Anonymous28-Jan-04 11:43 
GeneralRe: This includes no source code Pin
wiebe228-Jan-04 12:05
wiebe228-Jan-04 12:05 
GeneralRe: This includes no source code Pin
Mihail Stefanov28-Jan-04 22:38
Mihail Stefanov28-Jan-04 22:38 
GeneralRe: This includes no source code Pin
wiebew29-Jan-04 9:39
wiebew29-Jan-04 9:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.