Click here to Skip to main content
Licence 
First Posted 8 Jan 2004
Views 213,198
Bookmarked 50 times

Binding a two dimensional array to a DataGrid

By | 1 Feb 2004 | Article
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

About the Author

Mihail Stefanov



United States United States

Member

http://www.mommosoft.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalretrieve radgrid information PinmemberSaraMySara1:01 4 May '11  
GeneralThanks PinmemberJohann Medina10:07 19 Apr '09  
QuestionSpeed question PinmemberRealFractalizeR4:01 1 Apr '09  
AnswerRe: Speed question PinmemberMihail Stefanov3:29 2 Apr '09  
QuestionLicense? PinmemberHenrik Schmiediche15:57 27 Jan '09  
AnswerRe: License? PinmemberMihail Stefanov22:20 27 Jan '09  
GeneralRe: License? PinmemberHenrik Schmiediche9:11 28 Jan '09  
GeneralRe: License? PinmemberMihail Stefanov1:12 30 Jan '09  
QuestionHow to enable Searching, Sorting and grouping on your special object? Pinmembersun211706:18 9 Dec '08  
AnswerRe: How to enable Searching, Sorting and grouping on your special object? PinmemberMihail Stefanov23:37 9 Dec '08  
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pinmembersun211705:32 10 Dec '08  
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pinmembersun211705:59 10 Dec '08  
GeneralRe: How to enable Searching, Sorting and grouping on your special object? Pinmembersun211707:03 10 Dec '08  
GeneralRe: How to enable Searching, Sorting and grouping on your special object? PinmemberMihail Stefanov23:53 12 Dec '08  
Questionmommo.data works 80% of the time Pinmembersmcirish4:08 1 Aug '07  
AnswerRe: mommo.data works 80% of the time PinmemberMihail Stefanov4:52 1 Aug '07  
QuestionRe: mommo.data works 80% of the time Pinmembersmcirish4:55 1 Aug '07  
AnswerRe: mommo.data works 80% of the time PinmemberMihail Stefanov5:02 1 Aug '07  
QuestionRe: mommo.data works 80% of the time Pinmembersmcirish5:08 1 Aug '07  
QuestionRe: mommo.data works 80% of the time Pinmembersmcirish9:13 1 Aug '07  
AnswerRe: mommo.data works 80% of the time PinmemberMihail Stefanov9:41 1 Aug '07  
QuestionRe: mommo.data works 80% of the time [modified] Pinmembersmcirish10:00 1 Aug '07  
OK,
 
How do I check for object type in VB.net.
I'm using this to get the length of the array, it is = 3484
 
temp2 = Session("AR3_non_site").length
 
-smc
 

-- modified at 16:09 Wednesday 1st August, 2007
AnswerRe: mommo.data works 80% of the time PinmemberMihail Stefanov10:11 1 Aug '07  
GeneralRe: mommo.data works 80% of the time Pinmembersmcirish10:21 1 Aug '07  
QuestionRe: mommo.data works 100% of the time Pinmembersmcirish11:17 1 Aug '07  

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.

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