Click here to Skip to main content
11,439,025 members (79,568 online)
Click here to Skip to main content

Binding a two dimensional array to a DataGrid

, 1 Feb 2004
Rate this:
Please Sign up or sign in to vote.
Binding a two dimensional array to a DataGrid

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.

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

Using the code

It is very simple.

//
// 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 kind 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

  • 07.01.2004. Created.
  • 29.01.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

Share

About the Author

Mihail Stefanov

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

Comments and Discussions

 
QuestionHow to reset Pin
Doug Hagedorn6-Apr-15 16:36
memberDoug Hagedorn6-Apr-15 16:36 
GeneralIt suits perfect Pin
Member 109243964-Jul-14 1:56
memberMember 109243964-Jul-14 1:56 
General:) Pin
przemek1711-Mar-14 11:04
memberprzemek1711-Mar-14 11:04 
QuestionHow it is possible to use this library for one dimensional arrays? Pin
Kvanttt19-Mar-13 2:34
memberKvanttt19-Mar-13 2:34 
GeneralMy vote of 5 Pin
paulsasik12-Mar-13 12:14
memberpaulsasik12-Mar-13 12:14 
Generalretrieve radgrid information Pin
SaraMySara4-May-11 2:01
memberSaraMySara4-May-11 2:01 
GeneralThanks Pin
Johann Medina19-Apr-09 11:07
memberJohann Medina19-Apr-09 11:07 
QuestionSpeed question Pin
RealFractalizeR1-Apr-09 5:01
memberRealFractalizeR1-Apr-09 5:01 
AnswerRe: Speed question Pin
Mihail Stefanov2-Apr-09 4:29
memberMihail Stefanov2-Apr-09 4:29 
QuestionLicense? Pin
Henrik Schmiediche27-Jan-09 16:57
memberHenrik Schmiediche27-Jan-09 16:57 
AnswerRe: License? Pin
Mihail Stefanov27-Jan-09 23:20
memberMihail Stefanov27-Jan-09 23:20 
GeneralRe: License? Pin
Henrik Schmiediche28-Jan-09 10:11
memberHenrik Schmiediche28-Jan-09 10:11 
GeneralRe: License? Pin
Mihail Stefanov30-Jan-09 2:12
memberMihail Stefanov30-Jan-09 2:12 
QuestionHow to enable Searching, Sorting and grouping on your special object? Pin
sun211709-Dec-08 7:18
membersun211709-Dec-08 7:18 
AnswerRe: How to enable Searching, Sorting and grouping on your special object? Pin
Mihail Stefanov10-Dec-08 0:37
memberMihail Stefanov10-Dec-08 0:37 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pin
sun2117010-Dec-08 6:32
membersun2117010-Dec-08 6:32 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pin
sun2117010-Dec-08 6:59
membersun2117010-Dec-08 6:59 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pin
sun2117010-Dec-08 8:03
membersun2117010-Dec-08 8:03 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pin
Mihail Stefanov13-Dec-08 0:53
memberMihail Stefanov13-Dec-08 0:53 
Questionmommo.data works 80% of the time Pin
smcirish1-Aug-07 5:08
membersmcirish1-Aug-07 5:08 
AnswerRe: mommo.data works 80% of the time Pin
Mihail Stefanov1-Aug-07 5:52
memberMihail Stefanov1-Aug-07 5:52 
QuestionRe: mommo.data works 80% of the time Pin
smcirish1-Aug-07 5:55
membersmcirish1-Aug-07 5:55 
AnswerRe: mommo.data works 80% of the time Pin
Mihail Stefanov1-Aug-07 6:02
memberMihail Stefanov1-Aug-07 6:02 
QuestionRe: mommo.data works 80% of the time Pin
smcirish1-Aug-07 6:08
membersmcirish1-Aug-07 6:08 
QuestionRe: mommo.data works 80% of the time Pin
smcirish1-Aug-07 10:13
membersmcirish1-Aug-07 10:13 
AnswerRe: mommo.data works 80% of the time Pin
Mihail Stefanov1-Aug-07 10:41
memberMihail Stefanov1-Aug-07 10:41 
QuestionRe: mommo.data works 80% of the time [modified] Pin
smcirish1-Aug-07 11:00
membersmcirish1-Aug-07 11:00 
AnswerRe: mommo.data works 80% of the time Pin
Mihail Stefanov1-Aug-07 11:11
memberMihail Stefanov1-Aug-07 11:11 
GeneralRe: mommo.data works 80% of the time Pin
smcirish1-Aug-07 11:21
membersmcirish1-Aug-07 11:21 
QuestionRe: mommo.data works 100% of the time Pin
smcirish1-Aug-07 12:17
membersmcirish1-Aug-07 12:17 
GeneralLibrary Code Pin
Anil Gakhare26-Feb-07 0:35
memberAnil Gakhare26-Feb-07 0:35 
GeneralRe: Library Code Pin
Mihail Stefanov14-Mar-07 23:23
memberMihail Stefanov14-Mar-07 23:23 
GeneralProblem with the code Pin
wc0177-Sep-06 3:02
memberwc0177-Sep-06 3:02 
GeneralColumn Styles Pin
pczurak22-Mar-06 20:12
memberpczurak22-Mar-06 20:12 
GeneralThanks a lot !!!!!!! Pin
Krishna Baderia15-Mar-06 10:32
memberKrishna Baderia15-Mar-06 10:32 
GeneralIt wont work for me (shocker) Pin
pmauriello15-Feb-06 11:03
memberpmauriello15-Feb-06 11:03 
GeneralRe: It wont work for me (shocker) Pin
Mihail Stefanov15-Feb-06 11:07
memberMihail Stefanov15-Feb-06 11:07 
GeneralRe: It wont work for me (shocker) Pin
pmauriello15-Feb-06 11:54
memberpmauriello15-Feb-06 11:54 
GeneralRe: It wont work for me (shocker) Pin
pmauriello15-Feb-06 12:00
memberpmauriello15-Feb-06 12:00 
GeneralRe: It wont work for me (shocker) Pin
Mihail Stefanov15-Feb-06 13:16
memberMihail Stefanov15-Feb-06 13:16 
Questiontrouble with ArrayDataView Pin
smcirish16-Jul-07 5:55
membersmcirish16-Jul-07 5:55 
AnswerRe: trouble with ArrayDataView Pin
Mihail Stefanov17-Jul-07 11:15
memberMihail Stefanov17-Jul-07 11:15 
GeneralRe: It wont work for me (shocker) Pin
Mihail Stefanov15-Feb-06 13:20
memberMihail Stefanov15-Feb-06 13:20 
GeneralRe: It wont work for me (shocker) Pin
pmauriello15-Feb-06 13:28
memberpmauriello15-Feb-06 13:28 
GeneralHandling KeyPress event Pin
hellamasta8-Feb-06 23:47
memberhellamasta8-Feb-06 23:47 
GeneralRe: Handling KeyPress event Pin
Mihail Stefanov15-Feb-06 11:07
memberMihail Stefanov15-Feb-06 11:07 
GeneralRe: Handling KeyPress event Pin
hellamasta7-Mar-06 4:11
memberhellamasta7-Mar-06 4:11 
GeneralReading .dat file data into an array Pin
Slowie11-Jan-06 23:56
memberSlowie11-Jan-06 23:56 
GeneralRe: Reading .dat file data into an array Pin
Mihail Stefanov17-Jan-06 22:26
memberMihail Stefanov17-Jan-06 22:26 
QuestionHow to handle editing in DataGrid? Pin
Inkus15-Dec-05 5:58
memberInkus15-Dec-05 5:58 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150506.1 | Last Updated 2 Feb 2004
Article Copyright 2004 by Mihail Stefanov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid