KB Article T1005602
Visible to All Users

Add, Remove, and Initialize Rows – WinForms Cheat Sheet

DevExpress WinForms Cheat Sheets, Best Practices and Troubleshooting


Our data-aware controls (Gantt Control, Data Grid, Tree List, Vertical Grid) feature similar APIs to add, remove, and initialize rows. These APIs utilize public methods of controls' connected data sources (the IBindingList.AddNew and IList.Remove methods), so if an underlying data source does not support a specific operation, neither does our data-aware control. For example, if a control is bound to a data source that implements the IBindingList interface, the row operations’ availability depends on the AllowNew and AllowRemove properties. Also, to be able to add a new empty row, your business object should have a parameterless constructor.

The following table illustrates methods that you can call to add, remove, and initialize records inside DevExpress data-aware controls:

Control Add a new record Remove a record Initialize new records
Data Grid ColumnView.AddNewRow ColumnView.DeleteRow ColumnView.InitNewRow
Tree List/Gantt Control TreeList.AppendNode TreeList.DeleteNode(TreeListNode) TreeList.InitNewRow
Vertical Grid VGridControlBase.AddNewRecord VGridControlBase.DeleteRecord VGridControlBase.InitNewRecord
Scheduler Control AppointmentDataStorage.Add (use the SchedulerDataStorage.Appointments property to access the AppointmentDataStorage) AppointmentDataStorage.Remove(Appointment) SchedulerControl.InitNewAppointment

In the InitNewRow event handler, use NewItemRowHandle when getting / setting a value of a new row. Refer to the following cheat sheet to learn how to access, modify, and edit row/cell values: DevExpress WinForms Cheat Sheet - Access, Modify and Validate Rows and Cells of a Data-Aware control.

At runtime, users can add new records via data-aware controls' UI elements:

Control UI Element
Data Grid New Item Row, or the Append button of the data navigator
Tree List/Gantt Control New Item Row, or the Add Node / Add Child Node command in the context menu, or the Append button of the data navigator
Vertical Grid the Append button of the data navigator
Scheduler Control the New Appointment item of the context menu, or the in-place editor

DataNavigator can also be used to navigate in Data Layout Control that displays the current record. To add and remove rows, DataNavigator uses the CurrencyManager.AddNew and CurrencyManager.RemoveAt methods of CurrencyManager corresponding to the data source assigned to the DataSource property. If the data source implements IBindingList, CurrencyManager uses IBindingList.AddNew and IBindingList.Remove.


Examples

Add/remove a row

To add or remove a record, use the corresponding Add~/Remove~ methods of the required control from the table above. For example, for the Data Grid control, use the following code:

C#
view.AddNewRow(); //... view.DeleteRow(view.FocusedRowHandle);

Initialize a specific field/fields of a new row

Handle the InitNewRow event and use the corresponding method to set the required value for a field of a new row:

C#
private void gridView1_InitNewRow(object sender, InitNewRowEventArgs e) { GridView view = sender as GridView; view.SetRowCellValue(e.RowHandle, requiredFieldName, value); }

How to change a cell value of a new row when a value of another field of this row changes

Handle the CellValueChanged event. In this event handler, check if a current row is a new item row (IsNewItemRow) and call the SetRowCellValue or SetFocusedRowCellValue method to set required values.

C#
private void GridView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) { var view = sender as GridView; if (view.IsNewItemRow(e.RowHandle) && e.Column.FieldName == field1) view.SetRowCellValue(e.RowHandle, field2, e.Value.ToString()); }

Help us improve this article

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.