Click here to Skip to main content
15,846,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai can any1 help me how to create two dimension array dynamically
Suppose the i retrieve value from datatable in following manner

date Value Name
02/02/2011 2 Rajesh
02/02/2011 3 Ram
03/02/20111 8 Rajesh
03/02/2011 6 Rakesk
03/02/2011 3 Ram

Now my array should be like this

ar[0][0] = 02/02/2011
ar[0][1] = 2
ar[0][2] = 3

ar[1][0] = 03/02/2011
ar[1][1] = 8
ar[1][2] = 6
ar[1][3] = 3

There will be two different types (let's assume your element type is integer): int[][] and int[,].

As you hardly understand topic, start with 2D array (the other way allows for jagged array):

int [,] array = new int[4, 12];
array[2, 11] = 212;

The jagged array int[][] is not really a 2D array: this is an array of integer arrays. Both inner and outer arrays are 1D. If you think about this words and experiment a bit, you will find a way around them: you initialize outer array (array of array with 1D rank R) and then do the cycle 0 to R and initialize every inner array with any (different!) lengths. That's why it's called "jagged": inner arrays all have different lengths.

Share this answer
string[,] arrDateID = new string[5,5];
arrDateID [0,0] = 02/02/2011;
arrDateID [0,1] = 2;

Share this answer
Sergey Alexandrovich Kryukov 8-Feb-11 2:03am    
Why would you duplicate existing answer?
Sunasara Imdadhusen 9-Feb-11 6:24am    
There was a scenario when i have clicked on "Add an Answer" at that time no body given any answer. but during answer typing it will take time during this time any body would be given answer for the question.

Did you get it??
See here[^].

You already have the record count so you can just generate the array by using
int[,] myArray = new int[m,n];
Share this answer
Sergey Alexandrovich Kryukov 8-Feb-11 2:04am    
If you also add a reference about jagged arrays, will be good...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900