Click here to Skip to main content
15,887,776 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 379.3K   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: mommo.data works 80% of the time Pin
Mihail Stefanov1-Aug-07 9:41
Mihail Stefanov1-Aug-07 9:41 
QuestionRe: mommo.data works 80% of the time [modified] Pin
smcirish1-Aug-07 10:00
smcirish1-Aug-07 10:00 
AnswerRe: mommo.data works 80% of the time Pin
Mihail Stefanov1-Aug-07 10:11
Mihail Stefanov1-Aug-07 10:11 
GeneralRe: mommo.data works 80% of the time Pin
smcirish1-Aug-07 10:21
smcirish1-Aug-07 10:21 
QuestionRe: mommo.data works 100% of the time Pin
smcirish1-Aug-07 11:17
smcirish1-Aug-07 11:17 
GeneralLibrary Code Pin
Anil Gakhare25-Feb-07 23:35
Anil Gakhare25-Feb-07 23:35 
GeneralRe: Library Code Pin
Mihail Stefanov14-Mar-07 22:23
Mihail Stefanov14-Mar-07 22:23 
GeneralProblem with the code Pin
wc0177-Sep-06 2:02
wc0177-Sep-06 2:02 
GeneralColumn Styles Pin
pczurak22-Mar-06 19:12
pczurak22-Mar-06 19:12 
GeneralThanks a lot !!!!!!! Pin
Krishna Baderia15-Mar-06 9:32
Krishna Baderia15-Mar-06 9:32 
GeneralIt wont work for me (shocker) Pin
pictureman21115-Feb-06 10:03
pictureman21115-Feb-06 10:03 
GeneralRe: It wont work for me (shocker) Pin
Mihail Stefanov15-Feb-06 10:07
Mihail Stefanov15-Feb-06 10:07 
GeneralRe: It wont work for me (shocker) Pin
pictureman21115-Feb-06 10:54
pictureman21115-Feb-06 10:54 
GeneralRe: It wont work for me (shocker) Pin
pictureman21115-Feb-06 11:00
pictureman21115-Feb-06 11:00 
GeneralRe: It wont work for me (shocker) Pin
Mihail Stefanov15-Feb-06 12:16
Mihail Stefanov15-Feb-06 12:16 
Questiontrouble with ArrayDataView Pin
smcirish16-Jul-07 4:55
smcirish16-Jul-07 4:55 
AnswerRe: trouble with ArrayDataView Pin
Mihail Stefanov17-Jul-07 10:15
Mihail Stefanov17-Jul-07 10:15 
GeneralRe: It wont work for me (shocker) Pin
Mihail Stefanov15-Feb-06 12:20
Mihail Stefanov15-Feb-06 12:20 
GeneralRe: It wont work for me (shocker) Pin
pictureman21115-Feb-06 12:28
pictureman21115-Feb-06 12:28 
GeneralHandling KeyPress event Pin
hellamasta8-Feb-06 22:47
hellamasta8-Feb-06 22:47 
GeneralRe: Handling KeyPress event Pin
Mihail Stefanov15-Feb-06 10:07
Mihail Stefanov15-Feb-06 10:07 
GeneralRe: Handling KeyPress event Pin
hellamasta7-Mar-06 3:11
hellamasta7-Mar-06 3:11 
GeneralReading .dat file data into an array Pin
Slowie11-Jan-06 22:56
Slowie11-Jan-06 22:56 
GeneralRe: Reading .dat file data into an array Pin
Mihail Stefanov17-Jan-06 21:26
Mihail Stefanov17-Jan-06 21:26 
QuestionHow to handle editing in DataGrid? Pin
Inkus15-Dec-05 4:58
Inkus15-Dec-05 4:58 

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.