3

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 Answers1

4

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

default
  • 11,485
  • 9
  • 66
  • 102
davinceleecode
  • 777
  • 3
  • 10
  • 31
  • It works fine with me. Must make sure to declare Bound Column Properties. In my case I created two columns which are ***`Name`***, ***`Address`***. Now on your button click event add this sample code ***`this.datagridview1.Rows.Add("nameTest","addressTest");`***, After this code executes, This must work. – davinceleecode Nov 27 '17 at 12:17
  • to elaborate the data in datagridview1, iterate over the datagridview1.Rows: foreach (DataGridViewRow row in dataGridView1.Rows) { ... } – Martin Oct 14 '21 at 11:46