Liam neesan
Liam neesan

Reputation: 2571

How to add new row in DevExpress Gridview using C# code in Winforms?

I wants to add new row in GridViewControl. I tried this following code. But It doesn't adding any rows.

I created Column FilterValues using RunDesigner.

And then I am trying to add records using button click function by following code

(gridControlMultiFilterValues.MainView as DevExpress.XtraGrid.Views.Grid.GridView).AddNewRow();
int newRowHandle = (gridControlMultiFilterValues.MainView as DevExpress.XtraGrid.Views.Grid.GridView).FocusedRowHandle;

(gridControlMultiFilterValues.MainView as DevExpress.XtraGrid.Views.Grid.GridView).SetRowCellValue(newRowHandle, fieldName: "FilterValues", _value: "3rd Party %");

(gridControlMultiFilterValues.MainView as DevExpress.XtraGrid.Views.Grid.GridView).UpdateCurrentRow();
(gridControlMultiFilterValues.MainView as DevExpress.XtraGrid.Views.Grid.GridView).RefreshData();

Upvotes: 0

Views: 16825

Answers (2)

esnezz
esnezz

Reputation: 669

First make that the GridView is bound to a datasource that supports adding new items and then try to add a row to the grid like this:

  private void CreatNewRow(int val1, int val2, int val3)
{
    gridView1.AddNewRow();

    int rowHandle = gridView1.GetRowHandle(gridView1.DataRowCount);
    if (gridView1.IsNewItemRow(rowHandle))
    {
        gridView1.SetRowCellValue(rowHandle, gridView1.Columns[0], val1);
        gridView1.SetRowCellValue(rowHandle, gridView1.Columns[1], val2);
        gridView1.SetRowCellValue(rowHandle, gridView1.Columns[2], val3);
    }
}

More information: https://www.devexpress.com/Support/Center/Question/Details/Q456331/add-new-row-to-gridview

Upvotes: 1

Niranjan Singh
Niranjan Singh

Reputation: 18290

I suggest you to go through documentation - Adding and Deleting Records

To add a new row to a View, you can use the ColumnView.AddNewRow method. This method is only supported for data sources implementing the System.ComponentModel.IBindingList interface. In other cases, you should use the methods provided by your data source to add new rows.

You should use data source methods to add, delete and modify individual rows. Some data sources (for instance, arrays and read-only collections) do not support adding or deleting rows. There are some limitation on different data sources.

If you are grid control bound with correct data source then please go through the TableView.AddNewRow article to get information about the AddNewRow method.

References:
Add new row programmatically
How to: Initialize the New Item Row with Default Values
How to add a Row at Runtime to Devexpress Gridview

If you working in unbound mode then please review this article:
Can the GridControl be used completely in unbound mode?

In this case you have to create custom data store which you have to maintain when you do any insert/update operation.

Upvotes: 0

Related Questions