ColumnView.AddNewRow() Method
Creates a new blank row within the View. A user can specify cell values and accept changes to add the row to the grid’s data source, or cancel the operation.
Namespace: DevExpress.XtraGrid.Views.Base
Assembly: DevExpress.XtraGrid.v24.2.dll
Declaration
Remarks
The AddNewRow
method creates a new row and focuses it.
Note
The data source must implement the IBindingList interface.
The view does not automatically add the created row to the data source. To add the row to the data source, the user should do one of the following:
- Specify cell values. Focus the last cell in the row and press Enter.
- Move focus to another row.
- Click the End Edit button in the Data Navigator.
The user can press ESC twice to cancel the operation.
Initialize the New Row
The AddNewRow
method fires the ColumnView.InitNewRow event. Handle this event to initialize the new row:
void GridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e) {
gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns["LastName"], "Enter name");
gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns["BirthDate"], DateTime.Today);
}
The following image shows the result:
Tip
The following help topic describes how to access row data: Modify and Validate Cell Values.
Add the New Row to the Grid’s Data Source (in Code)
Use the ColumnView.UpdateCurrentRow method to add the new row to the grid’s data source:
void AddRowButton_Click(object sender, EventArgs e) {
gridView1.AddNewRow();
// The InitNewRow event fires between these method calls
gridView1.UpdateCurrentRow();
}
void GridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e) {
gridView1.SetRowCellValue(GridControl.NewItemRowHandle, gridView1.Columns["FirstName"], "Enter name");
gridView1.SetRowCellValue(GridControl.NewItemRowHandle, gridView1.Columns["LastName"], "Enter name");
gridView1.SetRowCellValue(GridControl.NewItemRowHandle, gridView1.Columns["BirthDate"], DateTime.Today);
}
Note
The AddNewRow
method does not update the database. Read the following help topic for additional information: Post Data to an Underlying Data Source.
Note
Detail pattern Views do not contain data and they are never displayed within XtraGrid. So, the AddNewRow member must not be invoked for these Views. The AddNewRow member can only be used with Views that display real data within the Grid Control. Use the following methods to access these Views with which an end user interacts at runtime.
- GridControl.MainView - returns the top most View in a grid;
- GridControl.FocusedView - returns the focused View;
- GridControl.DefaultView - returns the currently maximized View;
- the sender parameter of View specific events;
- GridView.GetDetailView - returns a detail clone View for a specific master row.