Back to Devexpress

ColumnView.AddNewRow() Method

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-columnview-ecbb1041.md

latest6.6 KB
Original Source

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.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
public virtual void AddNewRow()
vb
Public Overridable Sub AddNewRow

Remarks

The AddNewRow method creates a new row and focuses it.

csharp
gridView1.AddNewRow();
vb
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:

  • 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:

csharp
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);
}
vb
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.

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:

csharp
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);
}
vb
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

Add and Remove Rows

InitNewRow

New Item Row/Card

NewItemRowPosition

DeleteRow(Int32)

ColumnView Class

ColumnView Members

DevExpress.XtraGrid.Views.Base Namespace