How to add a new row to datagridview programmatically

You can do: DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone(); row.Cells[0].Value = “XYZ”; row.Cells[1].Value = 50.2; yourDataGridView.Rows.Add(row); or: DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone(); row.Cells[“Column2”].Value = “XYZ”; row.Cells[“Column6”].Value = 50.2; yourDataGridView.Rows.Add(row); Another way: this.dataGridView1.Rows.Add(“five”, “six”, “seven”,”eight”); this.dataGridView1.Rows.Insert(0, “one”, “two”, “three”, “four”); From: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx

Null Reference Exception when int parsing DataGridView row values

Of course the programming languge isnt wrong. Its reporting to you that you are tring to use an object reference which is in fact null. In the section dgvSelected.Rows[i].Cells[3].Value.ToString() one of the following is null dgvSelected dgvSelected.Rows dgvSelected.Rows[i] dgvSelected.Rows[i].Cells dgvSelected.Rows[i].Cells[3] dgvSelected.Rows[i].Cells[3].Value As you can see, you should be doing alot of checking to make sure … Read more