2-dimensional Integer array for DataGridView

How do I go about mapping a 2-dimensional integer array to a DataGridView control in C # .Net 4.0?

+9
c # datagridview
Nov 06 2018-10-06T00:
source share
2 answers

Follow the sample code on this page to populate the Rows property:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.aspx

Edit

Turns out it's a bit more creepy than I thought. Here is a sample code:

 var data = new int[4,3] { { 1, 2, 3, }, { 4, 5, 6, }, { 7, 8, 9, }, { 10, 11, 12 }, }; var rowCount = data.GetLength(0); var rowLength = data.GetLength(1); for (int rowIndex = 0; rowIndex < rowCount; ++rowIndex) { var row = new DataGridViewRow(); for(int columnIndex = 0; columnIndex < rowLength; ++columnIndex) { row.Cells.Add(new DataGridViewTextBoxCell() { Value = data[rowIndex, columnIndex] }); } dataGridView1.Rows.Add(row); } 
+17
Nov 06 2018-10-11T00:
source share
— -

in order to get Merlyn's solution, you will need to set the column counter before adding rows to the datagridview:

 dataGridView1.ColumnCount = 3; 
+11
Dec 28 '10 at 13:28
source share



All Articles