Click here to Skip to main content
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

 
GeneralIt suits perfect PinmemberMember 109243964-Jul-14 0:56 
General:) Pinmemberprzemek1711-Mar-14 10:04 
QuestionHow it is possible to use this library for one dimensional arrays? PinmemberKvanttt19-Mar-13 1:34 
GeneralMy vote of 5 Pinmemberpaulsasik12-Mar-13 11:14 
Generalretrieve radgrid information PinmemberSaraMySara4-May-11 1:01 
GeneralThanks PinmemberJohann Medina19-Apr-09 10:07 
QuestionSpeed question PinmemberRealFractalizeR1-Apr-09 4:01 
AnswerRe: Speed question PinmemberMihail Stefanov2-Apr-09 3:29 
QuestionLicense? PinmemberHenrik Schmiediche27-Jan-09 15:57 
AnswerRe: License? PinmemberMihail Stefanov27-Jan-09 22:20 
GeneralRe: License? PinmemberHenrik Schmiediche28-Jan-09 9:11 
GeneralRe: License? PinmemberMihail Stefanov30-Jan-09 1:12 
QuestionHow to enable Searching, Sorting and grouping on your special object? Pinmembersun211709-Dec-08 6:18 
AnswerRe: How to enable Searching, Sorting and grouping on your special object? PinmemberMihail Stefanov9-Dec-08 23:37 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pinmembersun2117010-Dec-08 5:32 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pinmembersun2117010-Dec-08 5:59 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pinmembersun2117010-Dec-08 7:03 
GeneralRe: How to enable Searching, Sorting and grouping on your special object? PinmemberMihail Stefanov12-Dec-08 23:53 
Questionmommo.data works 80% of the time Pinmembersmcirish1-Aug-07 4:08 
AnswerRe: mommo.data works 80% of the time PinmemberMihail Stefanov1-Aug-07 4:52 
QuestionRe: mommo.data works 80% of the time Pinmembersmcirish1-Aug-07 4:55 
AnswerRe: mommo.data works 80% of the time PinmemberMihail Stefanov1-Aug-07 5:02 
QuestionRe: mommo.data works 80% of the time Pinmembersmcirish1-Aug-07 5:08 
QuestionRe: mommo.data works 80% of the time Pinmembersmcirish1-Aug-07 9:13 
AnswerRe: mommo.data works 80% of the time PinmemberMihail Stefanov1-Aug-07 9:41 

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 | Mobile
Web01 | 2.8.140827.1 | Last Updated 2 Feb 2004
Article Copyright 2004 by Mihail Stefanov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid