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
NuGet Packages: DevExpress.Win.Grid, DevExpress.Win.Navigation
#Declaration
#Remarks
The AddNewRow
method creates a new row and focuses it.
Note
The data source must implement the IBinding
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 Add
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 Xtra
- Grid
Control. - returns the top most View in a grid;Main View - Grid
Control. - returns the focused View;Focused View - Grid
Control. - returns the currently maximized View;Default View - the sender parameter of View specific events;
- Grid
View. - returns a detail clone View for a specific master row.Get Detail View