C# Two-dimension Array and DataGridView

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p align=left><font face=Arial size=2></font> 
Hi dear Users.
<p align=left> 
<p align=left>Im a beginner programmer of the C#.
<p align=left>During the learning of this language Ive found some problems I cant solve myself.
<p align=left> 
<p align=left>Here we have simple 2D dynamic array "myTable" code:
<p align=left>... <font size=2>
</font><font color="#0000ff" size=2>private</font><font size=2> </font><font color="#0000ff" size=2>void</font><font size=2> button1_Click(</font><font color="#0000ff" size=2>object</font><font size=2> sender, </font><font color="#008080" size=2>EventArgs</font><font size=2> e)
{
</font><font color="#0000ff" size=2>int</font><font size=2> x = 3;
</font><font color="#0000ff" size=2>int</font><font size=2> y = 3;
</font><font color="#008080" size=2>Random</font><font size=2> rnd = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#008080" size=2>Random</font><font size=2>();

</font><font color="#0000ff" size=2>int</font><font size=2>[,] myTable = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#0000ff" size=2>int</font><font size=2>[x, y];
</font><font color="#0000ff" size=2>for</font><font size=2> (</font><font color="#0000ff" size=2>int</font><font size=2> i = 0; i != myTable.GetLength(0); i++)
{
</font><font color="#0000ff" size=2>for</font><font size=2> (</font><font color="#0000ff" size=2>int</font><font size=2> j = 0; j != myTable.GetLength(1); j++)
{
myTable[i, j] = rnd.Next(0,100);
textBox1.AppendText(</font><font color="#800000" size=2>"["</font><font size=2> + </font><font color="#008080" size=2>Convert</font><font size=2>.ToString(i) + </font><font color="#800000" size=2>","</font><font size=2> + </font><font color="#008080" size=2>Convert</font><font size=2>.ToString(j) + </font><font color="#800000" size=2>"]="</font><font size=2> + </font><font color="#008080" size=2>Convert</font><font size=2>.ToString(myTable[i, j]));
textBox1.AppendText(</font><font color="#008080" size=2>Environment</font><font size=2>.NewLine);
}
}
} </font>
<p align=left>...
<p align=left>Cells of the myTable gets random values. I can see array content in textBox.
<p align=left>Ive been trying to fill dataGridView control with "myTable". It should look like this below
<p align=left> 
<p align=left>            Column1   |   Column2   | Column3 | .... | ColumnX
<p align=left>---------------------------------------------------------------------
<p align=left>Row1   |   46         |      55          |   87        |.....
<p align=left>----------------------------------------------------------------------
<p align=left>Row2   |    4          |   8               |  34         |.....
<p align=left>----------------------------------------------------------------------
<p align=left>Row3   |    56        |   21             |  66          |.....
<p align=left>----------------------------------------------------------------------
......
<p align=left>----------------------------------------------------------------------
<p align=left>RowY
<p align=left> 
<p align=left>I have no idea how to add columns and rows dynamicly depending on "myTable" array size (x,y).
If someone know how to help me, please dont hesitate and write
<p align=left> 
<p align=left>Thanks fol all answers!
<p align=left> 
<p align=left>Best regards
<p align=left>Polix

View the full article
 
Back
Top