CHAPTER 22 ADO.NET PART II: THE DISCONNECTED LAYER
Figure 22-7. Updating the UI to enable removal of rows from the underlying DataTable
The following logic behind the new Button’s Click event handler removes the user-specified row,
based on the ID of a car, from your in-memory DataTable. The Select() method of the DataTable class
allows you to specify a search criteria, which is modeled after normal SQL syntax. The return value is an
array of DataRow objects that matches the search criteria.
// Remove this row from the DataRowCollection.
private void btnRemoveCar_Click (object sender, EventArgs e)
{
try
{
// Find the correct row to delete.
DataRow[] rowToDelete = inventoryTable.Select(
string.Format("ID={0}", int.Parse(txtCarToRemove.Text)));
// Delete it!
rowToDelete[0].Delete();
inventoryTable.AcceptChanges();
}
882
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}