I have a problem with datagridview.Rows.Add method. I want to add a new line but in the last line, not in previous. I don't know why the method Add doesn't work in this case. My code:
dataGridView1.RowCount = 3;
dataGridView1.ColumnCount = 4;
dataGridView1.Rows[0].Cells[0].Value = 1;
dataGridView1.Rows[0].Cells[1].Value = 2;
dataGridView1.Rows[0].Cells[2].Value = 3;
dataGridView1.Rows[0].Cells[3].Value = 4;
dataGridView1.Rows[1].Cells[0].Value = 5;
dataGridView1.Rows[1].Cells[1].Value = 6;
dataGridView1.Rows[1].Cells[2].Value = 7;
dataGridView1.Rows[1].Cells[3].Value = 8;
dataGridView1.Rows[2].Cells[0].Value = 9;
dataGridView1.Rows[2].Cells[1].Value = 10;
dataGridView1.Rows[2].Cells[2].Value = 11;
dataGridView1.Rows[2].Cells[3].Value = 12;
dataGridView1.Rows.Add(13,14,15,16);
And result:
I will be thrilled for any help or advice!
A better idea is not to set
RowCount
when you will be entering new rows. So if open to a alternate read on.In the example below there is no assertion to validate
row.Cells[3].Value
is an int but in the 2nd block shows how to check correctly.Assert cell value can represent an
int