0

How to Add new row in Devexpress gridview, When we set DataSource from List objlst I want to add new row at runtime.

private void SetData()
{
    List<CITEM> lstItem = new List<CITEM>();

    gridControl1.DataSource = lstItem;

    gridView1.PopulateColumns();
}

private void button1_Click(object sender, EventArgs e)
{
    gridView1.AddNewRow();
}
i3arnon
  • 113,022
  • 33
  • 324
  • 344
Akash
  • 21
  • 1
  • 4
  • When DataGridView is databound, you cannot add a new row. see here http://stackoverflow.com/questions/8708057/rows-cannot-be-programmatically-added-to-the-datagridviews-row-collection-when – Prateek Singh Jul 10 '12 at 06:05

2 Answers2

0

It will work if you change your list to a bindinglist

Stig
  • 1,323
  • 16
  • 22
  • 1
    Does CITEM have a parameterless constructor? If not, handle the AddNew event. See http://msdn.microsoft.com/en-us/library/ms132741. – Maarten Jul 10 '12 at 13:01
0
private void button1_Click(object sender, EventArgs e)
{
    object[] obj = //your row object;
    gridView1.AddNewRow();
    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0], obj[0].ToString());  
    gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[1], obj[1].ToString());  
    gridView1.UpdateCurrentRow(); 
}
Moinul Islam
  • 469
  • 2
  • 9