Try this:
// Define
dataGridView1.ColumnCount = 2;
dataGridView1.Rows.Add();
// Assign
dataGridView1[0, 0].Value = "zero column zero row";
dataGridView1[1, 0].Value = "first column zero row";
dataGridView1[0, 1].Value = "zero column first row";
dataGridView1[1, 1].Value = "first column first row";
There is some "magic" that takes places behind the scenes. According to the MSDN document for the Cells property:
If the row does not contain any cells when this property is accessed, a new empty DataGridViewCellCollection will be created by a call to the CreateCellsInstance method.
By specifying the ColumnCount
and calling the Rows.Add
method the the DataGridView has enough meta information to automatically create new Rows when directly accessing the Cells property.
Philip Fourie
2010-03-06 12:32:25