windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-columnview-ecbb1041.md
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.v25.2.dll
NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation
public virtual void AddNewRow()
Public Overridable Sub AddNewRow
The AddNewRow method creates a new row and focuses it.
gridView1.AddNewRow();
gridView1.AddNewRow()
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:
The user can press ESC twice to cancel the operation.
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);
}
Private Sub GridView1_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs)
gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns("LastName"), "Enter name")
gridView1.SetRowCellValue(e.RowHandle, gridView1.Columns("BirthDate"), DateTime.Today)
End Sub
The following image shows the result:
Tip
The following help topic describes how to access row data: Modify and Validate Cell Values.
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);
}
Private Sub AddRowButton_Click(ByVal sender As Object, ByVal e As EventArgs)
gridView1.AddNewRow()
' The InitNewRow event fires between these method calls
gridView1.UpdateCurrentRow()
End Sub
Private Sub GridView1_InitNewRow(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs)
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)
End Sub
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.
See Also