How to add new line of row in devexpress gridcontr

2019-07-18 04:26发布

问题:

I want to add a new line of row when pressing a button. In datagridview it would be: datagridview1.Rows.Add()

What is the equivalent code for that in gridcontrol? Please help me.

回答1:

You cannot add a new row directly to your GridControl, since this is just a container for the views. However, if you're using a GridView inside your GridControl (or any other descendant of ColumnView), you can add a new row using AddNewRow() method.

(myGridcontrol.MainView as DevExpress.XtraGrid.Views.Grid.GridView).AddNewRow();

Link to documentation

EDIT: You can access your view in a different way, of course.



回答2:

The DevExpress GridControl must always be bound to a datasource: you cannot add rows directly to the GridControl object or its child GridViews.

Instead, you must bind your GridControl to a data source (via the GridControl.DataSource property), and add/remove rows via this data source.

See the 'Binding To Data' documentation at the DevExpress site for more information on the kinds of data sources that can be used with a GridControl.



回答3:

You can use AddNewRow to add new row and SetRowCellValue to insert value to that row.

yourgridViewName.AddNewRow();
yourgridViewName.SetRowCellValue(rowhandle,columnName,value);
gridViewMappedFileds.UpdateCurrentRow();

Put yourgridName.RowCount-1 for rowhandle to insert the row at last.Put gridViewMappedFileds.Columns["ColumnName"] to give your columnname.