Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Using the Row for New Records in the Windows Forms DataGridView Control

When you use a DataGridView for editing data in your application, you will often want to give your users the ability to add new rows of data to the data store. The DataGridView control supports this functionality by providing a row for new records, which is always shown as the last row. It is marked with an asterisk (*) symbol in its row header. The following sections discuss some of the things you should consider when you program with the row for new records enabled.

Use the AllowUserToAddRows property to indicate whether the row for new records is displayed. The default value of this property is true.

For the data bound case, the row for new records will be shown if the AllowUserToAddRows property of the control and the IBindingList.AllowNew property of the data source are both true. If either is false then the row will not be shown.

When the user selects the row for new records as the current row, the DataGridView control raises the DefaultValuesNeeded event.

This event provides access to the new DataGridViewRow and enables you to populate the new row with default data. For more information, see How to: Specify Default Values for New Rows in the Windows Forms DataGridView Control

The row for new records is contained in the DataGridView control's Rows collection but behaves differently in two respects:

When the row for new records is created, it is based on the row specified by the RowTemplate property. Any cell styles that are not specified for this row are inherited from other properties. For more information about cell style inheritance, see Cell Styles in the Windows Forms DataGridView Control.

The initial values displayed by cells in the row for new records are retrieved from each cell's DefaultNewRowValue property. For cells of type DataGridViewImageCell, this property returns a placeholder image. Otherwise, this property returns null. You can override this property to return a custom value. However, these initial values can be replaced by a DefaultValuesNeeded event handler when focus enters the row for new records.

The standard icons for this row's header, which are an arrow or an asterisk, are not exposed publicly. If you want to customize the icons, you will need to create a custom DataGridViewRowHeaderCell class.

The standard icons use the ForeColor property of the DataGridViewCellStyle in use by the row header cell. The standard icons are not rendered if there is not enough space to display them completely.

If the row header cell has a string value set, and if there is not enough room for both the text and icon, the icon is dropped first.

In unbound mode, new records will always be added to the end of the DataGridView even if the user has sorted the content of the DataGridView. The user will need to apply the sort again in order to sort the row to the correct position; this behavior is similar to that of the ListView control.

In data bound and virtual modes, the insertion behavior when a sort is applied will be dependent on the implementation of the data model. For ADO.NET, the row is immediately sorted into the correct position.

You cannot set the Visible property of this row to false. An InvalidOperationException is raised if this is attempted.

The row for new records is always created in the unselected state.

If you are implementing virtual mode, you will need to track when a row for new records is needed in the data model and when to roll back the addition of the row. The exact implementation of this functionality depends on the implementation of the data model and its transaction semantics, for example, whether commit scope is at the cell or row level. For more information, see Virtual Mode in the Windows Forms DataGridView Control.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.