Add And Remove Rows | WinForms Controls
Có thể bạn quan tâm
- May 01, 2025
- 9 minutes to read
Common Requirements
The Data Grid uses methods of the data source assigned to the GridControl.DataSource property to add and remove rows. The data source must be editable (not read-only). A data source that implements the IBindingList interface must have AllowNew and AllowRemove settings enabled.
Note
The Data Grid does not support lists with simple type objects (for example, List<string>). If you create a list of classes with a property of the required simple type with an empty default constructor, the Data Grid can delete existing rows but cannot add new rows.
New Item Row
The New Item Row allows users to add new rows. Use the GridOptionsView.NewItemRowPosition property to display the New Item Row in a Data Grid:
- C#
- VB.NET

When a user starts editing within the New Item Row, the Data Grid initializes a new row object and raises the ColumnView.InitNewRow event, which allows you to initialize the new row with default values.
The Data Grid adds a new row object to the data source in the following cases:
- The New Item Row loses focus.
- A user moves focus to the last cell in the New Item Row and presses the Enter key.
- A user clicks the End Edit button in the Data Navigator.
- The UpdateCurrentRow method is called.
Run Demo: Table View Run Demo: Use New Item Row to Add Rows
Tip
Use the GridControl.NewItemRowHandle constant to obtain the New Item Row‘s handle.
Add Rows in Code
AddNewRow Method
The AddNewRow() method creates a new (blank) row in the View and raises the InitNewRow event, which allows you to initialize the new row with default values.
Note
The data source must support the IBindingList interface.
- C#
- VB.NET
If the View displays the New Item Row, the AddNewRow method moves focus to the New Item Row. Otherwise, the new row is temporarily displayed below existing data rows.
Initialize a New Row
Handle the InitNewRow event to initialize cells in the New Item Row with default values. Use the SetRowCellValue method to specify cell values.
The View raises the ColumnView.InitNewRow event in the following cases:
- A user starts editing a cell in the New Item Row.
- The AddNewRow() method is called.
The following example adds a new row, initializes the new row with default values, and saves the new row to the grid’s data source:
- C#
- VB.NET
Run Demo: Init New Rows
Tip
Read the following help topic for additional information: Get and Modify Cell Values in Code.
Validate a New Row
Handle the ColumnView.ValidateRow event to validate cell values in the new row before the row is added to the grid’s data source.
Example: Add a New Row to the Focused Data Group
The following example adds a new row to the focused data group. Data within the Grid View must be grouped against one or more columns.
- C#
- VB.NET

Example: Add a New Row on an Enter/Tab Press in the Last Cell
This example adds a new row when a user finishes editing the last data cell.
View Example

Add Rows using Data Source API
The code below adds a new (blank) record to the BindingList<T> that serves as a data source for a Data Grid:
- C#
- VB.NET
You can use data source API to add/remove rows if the data source implements such API.
If the data source does not implement the IBindingList interface, it does not send add/remove notifications to the Data Grid. In this case, use one of the following methods to update/refresh the Data Grid:
- GridControl.RefreshDataSource - updates the main view.
- View.RefreshData - updates a specific view.
Tip
Enable the GridOptionsNavigation.AutoFocusNewRow option to automatically focus the new row. This option is in effect if the data source sends add/remove notifications to the Data Grid.
Delete Rows
Delete a Row
Use the DeleteRow(Int32) method to delete the specified data row. The data source must allow delete operations.
Note
- In a Master-Detail Grid, the DeleteRow method deletes the master row together with its child rows.
- The DeleteRow method deletes a group row and all data rows in the group.
A DeleteRow(Int32) method call triggers the ColumnView.RowDeleting event. A successful delete operation raises the ColumnView.RowDeleted event.
The following code deletes the focused row when a user presses the Ctrl+Del keyboard shortcut:
- C#
- VB.NET
Delete Selected Rows
Use the DeleteSelectedRows() method to delete selected rows (or rows with selected cells if multiple cell selection mode is enabled).
A DeleteSelectedRows() method call triggers the ColumnView.RowDeleting event for each row to be deleted. Every successful delete operation raises the ColumnView.RowDeleted event.
Run Demo: Remove Rows with Ctrl+Delete Run Demo: Prevent a Row From Being Deleted
Tip
Read the following help topic for additional information: Multiple Row and Cell Selection.
Data Navigators
The Data Grid integrates the Data Navigator that allows users to navigate and edit data. Enable the GridControl.UseEmbeddedNavigator option to display the Data Navigator.

View Example: Embedded Data Navigator: Create a Custom Button
You can also use a standalone ControlNavigator. Use the ControlNavigator.NavigatableControl property to bind the Control Navigator to a Data Grid:
- C#
- VB.NET

Post User Edits to the Database
The Data Grid, like other DevExpress data-aware controls (for example, Gantt Control, Vertical Grid, TreeList), does not interact with the database directly. The Data Grid operates with a data source connected to the database. The Data Grid saves user edits to the data source, but you need to manually post these changes to the database.
Handle the ColumnView.RowUpdated event to post modified row values to the database. The event fires in the following cases:
- A user leaves the edited row (for example, the user moves focus to another row).
- The UpdateCurrentRow() method is called.
Tip
Read the following help topic for additional information: Post Data to an Underlying Data Source.
The following code handles the RowUpdated event and calls the table adapter’s Update method to post changes to the database:
- C#
- VB.NET
Note
The RowUpdated event does not fire while an editor is active (the View.IsEditing property returns true). To trigger this event manually, call the BaseView.CloseEditor method. The CloseEditor method saves changes, closes the editor, and calls the View’s UpdateCurrentRow() method.
Tip
Read the following help topics for additional information about row validation:
- Manage User Input
- How to: Validate Modified Rows
Example: Add Custom Data Rows
This example displays custom rows (ordinal, blank/group, and summary) in the Data Grid. Custom rows do not exist in the grid’s data source. Custom rows support data editing, sorting, and grouping.
The example creates a data source wrapper and handles the following events:
- The CustomColumnSort event is handled to display custom rows at the specified position regardless of sorting.
- The ShowingEditor event is handled to prevent editing blank and summary custom rows.
- The CustomColumnGroup event is handled to group custom rows. The CustomDrawGroupRow event is handled to display text within a custom group row.
View Example
Cheat Sheets and Best Practices
DevExpress WinForms UI controls introduce similar API to add, remove, and initialize rows. Read the following quick-reference guides for detailed information:
Add, Remove, and Initialize Rows
Từ khóa » Visual Studio Datagridview Add Row
-
Add Row To Datagridview Programmatically Code Example
-
DataGridView Adding Rows And Columns In VB.NET
-
Datagridview Add Rows - YouTube
-
How Do I Add Records To A DataGridView In VB.Net? - Stack Overflow
-
DataGridView.Rows Property (System.Windows.Forms)
-
DataGridViewRowCollection.Add Method (System.Windows.Forms)
-
Adding New Row In Datagridview [SOLVED] - - Daniweb
-
How To Add Row To DataGridView In Visual Basic (Visual Studio)
-
[Solved] Datagridview Add Empty Rows ? - CodeProject
-
Prevent (Disable) Adding New Row To DataGridView In Windows ...
-
Cách Thêm Một Hàng Mới Vào Datagridview Theo Chương Trình?
-
C# Controls Datagridview Add Row - C# - C# Tutorial - Wikitechy
-
C# DataGridView Add Rows - Dot Net Perls
-
Adding And Inserting Rows - WinForms GridView Control