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

Posted on 2009-02-18
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.
Question by:bencmorrow
    LVL 2

    Expert Comment

    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)?

            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 Comment

    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

    LVL 2

    Accepted Solution

    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


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video discusses moving either the default database or any database to a new volume.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now