Adding and Deleting Records

  • 3 minutes to read

This topic covers methods of adding and deleting records in the Vertical Grid Control (VGridControl).

Adding Records

To add a new record, you can use the VGridControlBase.AddNewRecord method. This method is only supported for data sources implementing the IBindingList interface. In other instances, to add new records you should use methods provided by your data source. After a new record has been added, the vertical grid reflects any changes made to the data source and focuses the new record if the VGridOptionsBehavior.AutoFocusNewRecord property is set to true.

The VGridControlBase.AddNewRecord method adds an empty record to the underlying data source. You can let the end-user to manually fill its cell values or initialize them in code using the VGridControlBase.SetCellValue method or by handling the VGridControlBase.InitNewRecord event.

Deleting Records

To delete a record from a data source, use the VGridControlBase.DeleteRecord method. This method is only in effect for data sources that support record deletion. The record to delete is identified by its index that is passed as a parameter.

Adding and Deleting Records Using Methods Provided by a Data Source

When working in bound mode, the most used types of data sources are DataTable and DataView objects. These objects represent records by means of the DataRow and DataRowView objects, respectively. The DataTable class exposes the collection of such objects via its Rows property. The DataView object represents the actual collection of these objects. You can use methods that add or remove objects from these collections to add and delete data source records.

The following example demonstrates how to add a new record to the data source which is represented by the DataTable object.

DataTable sourceTable = vGridControl1.DataSource as DataTable;
DataRow row = sourceTable.NewRow();
row["Trademark"] = "BMW";
row["Model"] = "530i";
row["Price"] = 39450;
sourceTable.Rows.Add(row);

The next example demonstrates how to add records when a data source is represented by a DataView object.

DataView sourceView = vGridControl1.DataSource as DataView;
DataRowView rowView = sourceView.AddNew();
rowView["Trademark"] = "BMW";
rowView["Model"] = "530i";
rowView["Price"] = 39450;
rowView.EndEdit();

To delete a record, you can use the DataTable.Rows.RemoveAt and DataView.Delete methods depending on the data source’s type. The following code handles the vertical grid’s KeyDown event to delete the currently focused record when the CTRL+DELETE combination is pressed.

private void vGridControl1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
   VGridControl vGrid = sender as VGridControl;
   if (e.KeyCode == Keys.Delete && e.Control) {
      DataView sourceView = vGrid.DataSource as DataView;
      DialogResult result = MessageBox.Show("Delete record?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
      if (result == DialogResult.Yes)
         sourceView.Delete(vGrid.FocusedRecord);
   }
}