We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

How do I catch a Datagridview format exception and set cell to DBNull.value

bencmorrow
bencmorrow asked
on
Medium Priority
8,635 Views
Last Modified: 2013-12-17
I have a bound DataGridView with one of its columns bound to a float column of a datatable. When the contents of a cell in this column are deleted (the user deletes the contents by entering the cell and pressing the delete key on the keyboard) and the user naviagtes away from this cell I get a Format Exception. How do I catch this error and enter DBNull.value into the cell.

I can catch the dataerror event and call e.cancel to return to the original value but I want to set the cell value to null (or even 0.0) when the contents have been deleted.
Comment
Watch Question

Hi bencmorrow,

You can use the DataError event of your datagridview control to catch the exception and change the value in the datatable.
Do you have an Id column that can permit to find the corresponding row in the datatable (see non tested attached code example)?

Julien
        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            int id = (int)dgv[myColIdIndex, e.RowIndex].Value;
            DataTable dt = dgv.DataSource as DataTable;
            foreach (DataRow row in dt.Select("MyColIdName=" + id.ToString()))
                row[e.ColumnIndex] = System.DBNull.Value;
        }

Open in new window

Author

Commented:
JulienVan:
Thanks for reply but I only want to set the value in the cell which returned the error and not all the cells in this row.
I have used the following code to catch the error and insert the value. This code catches the error OK but doesn't reset the cells value to DBNull.value and hence the cursor will not exit the current cell because all i have done is eaten the exception.
private void collectionsDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {      
      if (e.Exception is FormatException)
      {
        collectionsDataGridView[e.ColumnIndex, e.RowIndex].Value = System.DBNull.Value;
      }
    }

Open in new window

Hi,
Yes that is why I proposed to change this value directly into the DataTable. But it means finding the row of your DataTable with a unique identifier of the line, and that is why I asked you if you had an Id column.

But there is finally a more simple solution, using the CellValidating event (see code snippet).
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            if (e.FormattedValue.ToString() == string.Empty) dgv[e.ColumnIndex, e.RowIndex].Value = System.DBNull.Value;
        }

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.