473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

arrays and datagrids

I wonder if someone could help me.

I've got a 2 Dimension Array that I'm working with and want to display
the results from this array in a datagrid.

I'm quite new to C# and seem unable to link the two together.

So, the question is, how do I set up my datagrid to accept the array
as the source?
Thanks,

Alan
Nov 15 '05 #1
3 1312
The datagrid component can display automatically array but only of one
dimension.
If you want to display a 2 dimensional array you must use a datatable that
contain data of your array.
I wrote for you this little function that receive an array of title, a 2
dimensional array and return a datatable with data of your array.
Insert this code in a new WindowsApplication project with a datagrid named
dt and use it.

private DataTable createDT(string[] title, object[,] array)

{

int row = 0, col = 0;

DataTable dt = new DataTable("MyArray");

DataRow dr;

for(col = 0; col < title.GetLength(0); col++)

dt.Columns.Add(title[col]);

for(row = 0; row < array.GetLength(0); row++)

{

dr = dt.NewRow();

for(col = 0; col < array.GetLength(1); col++)

dr[title[col]] = array[row, col];

dt.Rows.Add(dr);

}

return dt;

}
Then modify the Form1_load in this manner

private void Form1_Load(object sender, System.EventArgs e)

{

string[] title = new string[] {"Colonna1", "Colonna2", "Colonna3",
"Colonna4", "Colonna5"};

string[,] myArray = new string[5,5];

for(int x = 0; x < 5; x++)

for(int y = 0; y < 5; y++)

myArray[x,y] = (x * y).ToString();

dg.SetDataBinding(createDT(title, (object[,])myArray), null);

}

If you have made all how you must you can now see your array in the
datagrid.
I hope of to be useful for you.
Excuse my english but im italian and i dont know a good englis.

"alan" <al******@hotmail.com> ha scritto nel messaggio
news:e1**************************@posting.google.c om...
I wonder if someone could help me.

I've got a 2 Dimension Array that I'm working with and want to display
the results from this array in a datagrid.

I'm quite new to C# and seem unable to link the two together.

So, the question is, how do I set up my datagrid to accept the array
as the source?
Thanks,

Alan

Nov 15 '05 #2
Alan,

A jagged array isn't really a good candidate for data binding. Rather,
you should change your structure to a data table, and bind to that.

Two dimensional arrays can not be bound to by the data set. With a
jagged array, you have a one dimensional array whose elements are other one
dimensional arrays. Now you can bind to this, but you will see the
properties of each element of the first array, which is the properties for
an Array class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"alan" <al******@hotmail.com> wrote in message
news:e1**************************@posting.google.c om...
I wonder if someone could help me.

I've got a 2 Dimension Array that I'm working with and want to display
the results from this array in a datagrid.

I'm quite new to C# and seem unable to link the two together.

So, the question is, how do I set up my datagrid to accept the array
as the source?
Thanks,

Alan

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: news | last post by:
Can .NET handle the old system of arrays for controls used in Visual Basic? eg TextBox(1), (2) ...
1
by: Simon Harris | last post by:
Hi All, I wish to populate more than one datagrid from the same OleDBCommand. The code I have is: Dim objCmd As New OleDbCommand(strSql, objConn) Then... ...
4
by: ree32 | last post by:
I have a placeholder and depending on a user input(a drop downlist) when the user clicks a button I dynamically create a number of datagrids and fill them with data from a database. But the problem...
0
by: Scott Meddows | last post by:
I'm having trouble scrolling some datagrids so they sync up... I have two identical datagrids on a form, filled with eh same dataset. I want a user to be able to scroll on the datasets and the...
4
by: | last post by:
Hello. In SQL Server Query Analyzer, if I type in, say, 2 select queries, it returns 2 datagrids of results, with a scrollbar on the right to scroll between the 2 (or more) datagrids. I wanted to...
3
by: Mark Wiewel | last post by:
hi all, i am a newbie in ASP.NET and i couldn't find the solution to this one: i have a form with three datagrids on it. i would like to align them vertically with a space between each grid of...
7
by: Ausclad | last post by:
Ok, ill try again..... It seems fairly simple. I have two combo boxes in a datagrid. The datagrid is bound to a a table in a dataset. The two combo boxes are bound to a single data table...
2
by: rn5a | last post by:
In a shopping cart app, a ASPX page retrieves the order details & personal details of a user from a MS-Access database table depending upon the username of the user. The order details of a...
0
by: Jim | last post by:
OK here's my disclaimer: I'm very new to ASP.NET and posting on Google Groups, so please bear with me and feel free to correct either the way I code or the way I post. I have an ASP.NET page...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.