March 3, 2021
Hot Topics:

Working with the DataGridView Control

  • By Bipin Joshi
  • Send Email »
  • More Articles »

If you run the form now, you should see something similar to Figure 7.

Click here for a larger image.

Notice how the Country column shows a combo box from which you can pick up a country value. Also, see how the button column is displayed as a push button. At this stage, clicking the Delete button won't do anything. To add this functionality, you must handle a CellClick event. The following code shows what the CellClick event handler looks like:

private void dataGridView1_CellClick(object sender,
   DataGridViewCellEventArgs e)
   if (e.ColumnIndex >= 0)
      if (dataGridView1.Columns[e.ColumnIndex].Name == "Column5")

Inside the CellClick event handler, you check whether the name of the cell being clicked is Column5—the button column. If so, you call the RemoveAt() method of the Rows collection of the DataGridView. The DataGridViewCellEventArgs parameter received by the event handler supplies the row and column index.

Validating Data in DataGridView

In the examples above, tiy were able to edit the data at will. For example, even if the CustomerID column is the primary key column, you are able to change its values. Similarly, there is no check on the new values being entered in other columns. There are many properties and events of the DataGridView control that help you build a robust validation mechanism. To understand how this is done, implement the following constraints and validations:

  • The CustomerID column must be read only for the existing rows. However, a new row must be editable.
  • The newly entered CustomerID must be at least five characters long.
  • The user must be prompted with a confirmation message before deleting a row.

To implement the first constraint, open the "Edit Columns" dialog of the DataGridView control and set the ReadOnly property of the CustomerID column to True. This will ensure that the CustomerID column is non editable.

To implement the second requirement, you must handle the DefaultValuesNeeded event. The DefaultValuesNeeded event is raised when the user inserts a new row in the DataGridView. This event can be used to provide default values to the new record. In the example, you will use it to make CustomerID column editable. Write the following code in the DefaultValuesNeeded event handler.

private void dataGridView1_DefaultValuesNeeded(object sender,
   DataGridViewRowEventArgs e)
e.Row.Cells["Column1"].ReadOnly = false;

The DataGridViewRowEventArgs parameter gives a reference to the new row being added. You simply set the ReadOnly property of Column1 (CustomerID) to False. This way, the user can enter a value in the CustomerID column for just the new row.

To prompt the user about row deletion, you need to handle the UserDeletingRow event of the DataGridView control. This event is raised prior to deleting a row from the grid. You can cancel the delete operation if some validation fails. In your case, the UserDeletingRow event handler looks like this:

private void dataGridView1_UserDeletingRow(object sender,
   DataGridViewRowCancelEventArgs e)
   DialogResult result = MessageBox.Show("Do you want to delete
      this record?", "Warning", MessageBoxButtons.YesNo);
   if (result == DialogResult.No)
      e.Cancel = true;

Here, you display a message box to the user with a warning message. If the user decides not to delete the row, you set the Cancel property of DataGridViewRowCancelEventArgs parameter to True. Doing so cancels the delete operation. Note that the UserDeletingRow event is not raised if you remove a row using the RemoveAt() method. If you try to delete a row (by selecting it and pressing the delete key), you will see a message box, as shown below:

Click here for a larger image.

Page 4 of 5

This article was originally published on December 28, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date