Click here to Skip to main content
15,886,199 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.1K   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

 
QuestionHow to reset Pin
Doug Hagedorn6-Apr-15 15:36
Doug Hagedorn6-Apr-15 15:36 
GeneralIt suits perfect Pin
Member 109243964-Jul-14 0:56
Member 109243964-Jul-14 0:56 
General:) Pin
przemek1711-Mar-14 10:04
przemek1711-Mar-14 10:04 
QuestionHow it is possible to use this library for one dimensional arrays? Pin
Ivan Kochurkin19-Mar-13 1:34
Ivan Kochurkin19-Mar-13 1:34 
GeneralMy vote of 5 Pin
paulsasik12-Mar-13 11:14
professionalpaulsasik12-Mar-13 11:14 
Generalretrieve radgrid information Pin
SaraMySara4-May-11 1:01
SaraMySara4-May-11 1:01 
GeneralThanks Pin
Johann Medina19-Apr-09 10:07
Johann Medina19-Apr-09 10:07 
QuestionSpeed question Pin
VladislavRastrusny1-Apr-09 4:01
VladislavRastrusny1-Apr-09 4:01 
AnswerRe: Speed question Pin
Mihail Stefanov2-Apr-09 3:29
Mihail Stefanov2-Apr-09 3:29 
QuestionLicense? Pin
Henrik Schmiediche27-Jan-09 15:57
Henrik Schmiediche27-Jan-09 15:57 
AnswerRe: License? Pin
Mihail Stefanov27-Jan-09 22:20
Mihail Stefanov27-Jan-09 22:20 
GeneralRe: License? Pin
Henrik Schmiediche28-Jan-09 9:11
Henrik Schmiediche28-Jan-09 9:11 
GeneralRe: License? Pin
Mihail Stefanov30-Jan-09 1:12
Mihail Stefanov30-Jan-09 1:12 
QuestionHow to enable Searching, Sorting and grouping on your special object? Pin
sun211709-Dec-08 6:18
sun211709-Dec-08 6:18 
AnswerRe: How to enable Searching, Sorting and grouping on your special object? Pin
Mihail Stefanov9-Dec-08 23:37
Mihail Stefanov9-Dec-08 23:37 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pin
sun2117010-Dec-08 5:32
sun2117010-Dec-08 5:32 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pin
sun2117010-Dec-08 5:59
sun2117010-Dec-08 5:59 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pin
sun2117010-Dec-08 7:03
sun2117010-Dec-08 7:03 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pin
Mihail Stefanov12-Dec-08 23:53
Mihail Stefanov12-Dec-08 23:53 
Questionmommo.data works 80% of the time Pin
smcirish1-Aug-07 4:08
smcirish1-Aug-07 4:08 
20% of the time I receive this error message:

DataGrid with id 'Datagrid2' could not automatically generate any columns from the selected data source.

-sheila
AnswerRe: mommo.data works 80% of the time Pin
Mihail Stefanov1-Aug-07 4:52
Mihail Stefanov1-Aug-07 4:52 
QuestionRe: mommo.data works 80% of the time Pin
smcirish1-Aug-07 4:55
smcirish1-Aug-07 4:55 
AnswerRe: mommo.data works 80% of the time Pin
Mihail Stefanov1-Aug-07 5:02
Mihail Stefanov1-Aug-07 5:02 
QuestionRe: mommo.data works 80% of the time Pin
smcirish1-Aug-07 5:08
smcirish1-Aug-07 5:08 
QuestionRe: mommo.data works 80% of the time Pin
smcirish1-Aug-07 9:13
smcirish1-Aug-07 9:13 

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.