How to Catch All Datagrid Exceptions
- 1). Click the Visual Studio 2010 shortcut to load the program. The home page appears after a few moments. Click on the link labeled “New Project” to open the “New Project” window. In this window, select “C#” from the left column and “Windows Form Application” from the right column. Enter a name for the project and press the “OK” button. A blank Windows Form appears in the visual editor window.
- 2). Click on the “Toolbar” panel located to the right of the visual editor window. A list of all the tools compatible with Windows Forms appears.
- 3). Click on “DataGridView” and drag the mouse cursor over to the Windows Form in the visual editor. Release the mouse button to place a DataGridView.
- 4). Click on the “Properties” panel, which is located to the right of the visual editor window. At the top of this panel, there is a small lightning bolt icon. This represents the events the DataGridView is capable of performing. Click on this icon to list all of these events.
- 5). Double-click the DataError event to create an automatically generated method that handles DataGrid”exceptions. The visual editor is immediately replaced by a source code editor, which displays the automatically generated method. This method looks like this:
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
} - 6). Place the code below inside the curly brackets of the DataError method. This code catches all of the DataGrid exceptions and displays a message box with a simple statement whenever an exception occurs:
if (e.Exception != null &&
e.Context == DataGridViewDataErrorContexts.Commit)
{
MessageBox.Show("Exception Occured.");
} - 7). Press the green “Play” button located at the top of the Visual Studio interface to launch the program. A Windows Form appears and displays a DataGridView. If any exceptions occurs, a message box appears and states, “Exception Occurred.”
Source...