0

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();
1
  • Is gridControlMultiFilterValues bound do any datasource? Commented Aug 21, 2017 at 8:42

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

you did the copy paste. I already tried this one. It's not working
You should make sure that your GridView is bound to a DataSource that supports adding and editing such as BindingSource otherwise it won't work.
Also if your GridView is bound to a BindingSource, you can add a new item to your Datasource and the GridView will update itself to reflect the change.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.