3
votes

I try to add new row to datagridview ,I try by using this code

DataGridViewRow row = (DataGridViewRow)dgv_OfferTerms.Rows[0].Clone();
row.Cells[0].Value = cond.Id;
row.Cells[1].Value = cond.Name;
row.Cells[2].Value = cond.Title;
row.Cells[3].Value = cond.Description;
dgv_OfferTerms.Rows.Add(row);

it didn't work so i try this

dgv_OfferTerms.Rows.Add(cond.Id,cond.Name,cond.Title,cond.Description);

didn't work how can i add new row to datagridview??

1
Most of the time you will want to add rows to the datasource not the control. - Ňɏssa Pøngjǣrdenlarp
@Jimi I try this answer but it didn't work with me - Abd ul rahaman Shalata
@Abd ul rahaman Shalata Why? What happens when you add the new row? Are you sure you are feeding the right fields and field format? - Jimi

1 Answers

3
votes

These code may help you:

this.dataGridView1.Rows.Add("five", "six", "seven","eight");
this.dataGridView1.Rows.Insert(0, "one", "two", "three", "four");

or:

DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells["Column2"].Value = "XYZ";
row.Cells["Column6"].Value = 50.2;
yourDataGridView.Rows.Add(row);

Reference from:
https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rows.aspx