Data GridView Data Error Event

There are a lot of different places that things can go wrong with bound data in SataGridView control. The data that goes into the grid could come from direct input from the user if you allow the grid to support editing and adding new rows, or it could be programmatically changed behind the scenes. You could have complex cell types that have some error in their processing or presentation of their values.

The DataError event on the DataGridView lets you provide centralized processing code for handling errors of many different types that can occur to the data within a DataGridView control or from the underlying data source. The event passes an event argument of type DataGridViewDataErrorEventArgs, which carries a bunch of context information about the error along with it. This event argument type has the properties shown in Table 10.1.

Table 10.1. DataGridViewDataErrorEventArgs Class Properties

Name

Type

Description

Cancel

Boolean

If you set this property to true, other subscribers for this event will not be called; if false, they will. The default is false. This property is inherited from the CancelEventArgs base class.

Columnlndex

Integer

The cell's column index that caused the error.

Context

DataGridViewDataErrorContexts

An enumerated value that describes the context where the error occurred (see Table 10.2). This is a flags enumeration tvce, so it can take on any combination of the values shown in Table 10.2.

Exception

Exception

The exception that was thrown to cause the error.

Rowlndex

Integer

The row index where the problem occurred.

ThrowException

Boolean

If this property is true, your event handler should rethrow the exception after processing it. If false (the default), your handler shouldn't rethrow the exception.

Table 10.2. DataGridViewDataErrorContexts Flags Enumeration Values

Value

Description

ClipboardContent

An error occurred copying the cell contents to the Clipboard because it couldn't be converted to a string.

Commit

An error occurred when the grid tried to write the parsed value to the underlying data source.

CurrentCellChange

This error is caused by trying to change to a different cell when there is already an error in the current cell that hasn't been corrected yet.

Display

An error occurred when trying to paint the cell or the tooltip text. This usually means there is something wrong with the mapping of the column to the property or field in the data source.

Formatting

An error occurred when trying to format data as it is taken out of the data source and before it is displayed, possibly from any custom formatting code.

InitialValueRestoration

An error occurred when trying to roll back the cell value to its previous value, typically by the user pressing the Esc key.

Leave Control

An error occurred as the focus was leaving the grid, because the control couldn't save pending changes in the grid to the data source.

Parsing

An error occurred when trying to take the entered value from a cell and save it to the underlying data source, possibly from any custom parsing code.

PreferredSize

An error occurred when the grid tried to calculate the desired size of a cell based on its formatted contents.

RowDeletion

An error occurred when the grid tried to remove a row from the data source based on a deletion in the grid (usually by the user pressing the Del key).

Scroll

An error occurred when scrolling a cell into view, because of the formatting or display code invoked based on the cell becoming visible.

For example, if a property's get or set blocks in a custom data object that is bound to a row in the DataGridView control throw an exception, the DataError event will fire with a context value oDisplay or Commit, respectively.

As an example, consider if you bound a DataGridView control to a binding source, which was bound to aList<SimpleDataltem>. The SimpleDataltem class is defined as:

class SimpleDataltem {

private int m_SomeVal;

public int SomeVal {

get {throw new ArgumentException("foo");} set { m_SomeVal = value;}

private string m_SomeVar;

public string Var {

Then assume you added a handler to your form for the DataError event on the grid as follows:

private void OnDataError(object sender, DataGridViewDataErrorEventArgs e)

string msg = string. Format( "DataError occurred:\n{0}\n{1}\nDataErrorContext: {2}", e. Exception.GetType().ToString(),e. Exception. Message, e.Context); MessageBox.Show(msg);

As the DataGridView attempted to render the contents of theSomeVal property for each row, you would get theMessageBox shown in Figure 10.5.

Figure 10.5. DataError Event Handling

[View full size imagel

Figure 10.5. DataError Event Handling

DataError occurred;

iyitem.ftefleclruOfl.TaigetlnvpcatHjnELigeptDGn

+1 -1

Responses

  • PERTTI VEITONEN
    How to fire datagridview dataerror event?
    7 years ago
  • Miranda Mugwort
    How to handle the DataError event for the datagridview in visual basics?
    5 years ago

Post a comment