Click here to Skip to main content
12,505,592 members (53,906 online)
Click here to Skip to main content
Add your own
alternative version

Stats

289.4K views
10.8K downloads
53 bookmarked
Posted

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionHow to reset Pin
Doug Hagedorn6-Apr-15 15:36
memberDoug Hagedorn6-Apr-15 15:36 
GeneralIt suits perfect Pin
Member 109243964-Jul-14 0:56
memberMember 109243964-Jul-14 0:56 
General:) Pin
przemek1711-Mar-14 10:04
memberprzemek1711-Mar-14 10:04 
QuestionHow it is possible to use this library for one dimensional arrays? Pin
Kvanttt19-Mar-13 1:34
memberKvanttt19-Mar-13 1:34 
I wanna use this lib for double[] array (not for double[1, n]). Is it possible?
GeneralMy vote of 5 Pin
paulsasik12-Mar-13 11:14
memberpaulsasik12-Mar-13 11:14 
Generalretrieve radgrid information Pin
SaraMySara4-May-11 1:01
memberSaraMySara4-May-11 1:01 
GeneralThanks Pin
Johann Medina19-Apr-09 10:07
memberJohann Medina19-Apr-09 10:07 
QuestionSpeed question Pin
RealFractalizeR1-Apr-09 4:01
memberRealFractalizeR1-Apr-09 4:01 
AnswerRe: Speed question Pin
Mihail Stefanov2-Apr-09 3:29
memberMihail Stefanov2-Apr-09 3:29 
QuestionLicense? Pin
Henrik Schmiediche27-Jan-09 15:57
memberHenrik Schmiediche27-Jan-09 15:57 
AnswerRe: License? Pin
Mihail Stefanov27-Jan-09 22:20
memberMihail Stefanov27-Jan-09 22:20 
GeneralRe: License? Pin
Henrik Schmiediche28-Jan-09 9:11
memberHenrik Schmiediche28-Jan-09 9:11 
GeneralRe: License? Pin
Mihail Stefanov30-Jan-09 1:12
memberMihail Stefanov30-Jan-09 1:12 
QuestionHow to enable Searching, Sorting and grouping on your special object? Pin
sun211709-Dec-08 6:18
membersun211709-Dec-08 6:18 
AnswerRe: How to enable Searching, Sorting and grouping on your special object? Pin
Mihail Stefanov9-Dec-08 23:37
memberMihail Stefanov9-Dec-08 23:37 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pin
sun2117010-Dec-08 5:32
membersun2117010-Dec-08 5:32 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pin
sun2117010-Dec-08 5:59
membersun2117010-Dec-08 5:59 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pin
sun2117010-Dec-08 7:03
membersun2117010-Dec-08 7:03 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pin
Mihail Stefanov12-Dec-08 23:53
memberMihail Stefanov12-Dec-08 23:53 
Questionmommo.data works 80% of the time Pin
smcirish1-Aug-07 4:08
membersmcirish1-Aug-07 4:08 
AnswerRe: mommo.data works 80% of the time Pin
Mihail Stefanov1-Aug-07 4:52
memberMihail Stefanov1-Aug-07 4:52 
QuestionRe: mommo.data works 80% of the time Pin
smcirish1-Aug-07 4:55
membersmcirish1-Aug-07 4:55 
AnswerRe: mommo.data works 80% of the time Pin
Mihail Stefanov1-Aug-07 5:02
memberMihail Stefanov1-Aug-07 5:02 
QuestionRe: mommo.data works 80% of the time Pin
smcirish1-Aug-07 5:08
membersmcirish1-Aug-07 5:08 
QuestionRe: mommo.data works 80% of the time Pin
smcirish1-Aug-07 9:13
membersmcirish1-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.

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