DataGridViewDataErrorContexts DataGridViewDataErrorContexts DataGridViewDataErrorContexts DataGridViewDataErrorContexts Enum


表示进行了数据绑定的 DataGridView 控件在发生数据错误时的状态。Represents the state of a data-bound DataGridView control when a data error occurred.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

public enum class DataGridViewDataErrorContexts
public enum DataGridViewDataErrorContexts
type DataGridViewDataErrorContexts = 
Public Enum DataGridViewDataErrorContexts


ClipboardContent ClipboardContent ClipboardContent ClipboardContent 16384

将内容复制到剪贴板时发生了数据错误。A data error occurred when copying content to the Clipboard. 该值表示未能将单元格值转换为字符串。This value indicates that the cell value could not be converted to a string.

Commit Commit Commit Commit 512

向数据存储区提交更改时发生了数据错误。A data error occurred when committing changes to the data store. 该值表示未能将单元格中输入的数据提交到基础数据存储区。This value indicates that data entered in a cell could not be committed to the underlying data store.

CurrentCellChange CurrentCellChange CurrentCellChange CurrentCellChange 4096

将选择光标移到另一个单元格时发生了数据错误。A data error occurred when the selection cursor moved to another cell. 该值表示用户在以前选定的单元格有错误的情况下选择了另一个单元格。This value indicates that a user selected a cell when the previously selected cell had an error condition.

Display Display Display Display 2

显示由数据源填充的单元格时发生了数据错误。A data error occurred when displaying a cell that was populated by a data source. 该值表示该单元格无法显示来自数据源的值,或缺少将来自数据源的值转换为单元格的映射。This value indicates that the value from the data source cannot be displayed by the cell, or a mapping that translates the value from the data source to the cell is missing.

Formatting Formatting Formatting Formatting 1

尝试设置数据的格式时发生数据错误,该数据或者正发送到数据存储区,或者正从数据存储区加载。A data error occurred when trying to format data that is either being sent to a data store, or being loaded from a data store. 该值表示未能正确设置对单元格所做更改的格式。This value indicates that a change to a cell failed to format correctly. 需要更正新的单元格值,或者需要更改单元格的格式设置。Either the new cell value needs to be corrected or the cell's formatting needs to change.

InitialValueRestoration InitialValueRestoration InitialValueRestoration InitialValueRestoration 1024

将单元格还原到它以前的值时发生了数据错误。A data error occurred when restoring a cell to its previous value. 该值表示单元格尝试取消编辑,但回滚到初始值失败。This value indicates that a cell tried to cancel an edit and the rollback to the initial value failed. 如果单元格格式设置已更改,导致它与初始值不兼容,便可能会发生这种情况。This can occur if the cell formatting changed so that it is incompatible with the initial value.

LeaveControl LeaveControl LeaveControl LeaveControl 2048

DataGridView 失去焦点时发生了数据错误。A data error occurred when the DataGridView lost focus. 该值表示 DataGridView 在失去焦点后,未能提交用户更改。This value indicates that the DataGridView could not commit user changes after losing focus.

Parsing Parsing Parsing Parsing 256

分析新数据时发生了数据错误。A data error occurred when parsing new data. 该值表示 DataGridView 未能分析由用户输入或者从基础数据存储区加载的新数据。This value indicates that the DataGridView could not parse new data that was entered by the user or loaded from the underlying data store.

PreferredSize PreferredSize PreferredSize PreferredSize 4

计算单元格的首选大小时发生了数据错误。A data error occurred when calculating the preferred size of a cell. 该值表示在以编程方式调整列或行的大小时,DataGridView 未能计算单元格的首选宽度或高度。This value indicates that the DataGridView failed to calculate the preferred width or height of a cell when programmatically resizing a column or row. 如果单元格未能设置它所包含的值的格式,便可能会发生这种情况。This can occur if the cell failed to format its value.

RowDeletion RowDeletion RowDeletion RowDeletion 8

删除行时发生了数据错误。A data error occurred when deleting a row. 该值表示当数据绑定的 DataGridView 删除行时,基础数据存储区引发异常。This value indicates that the underlying data store threw an exception when a data-bound DataGridView deleted a row.

Scroll Scroll Scroll Scroll 8192

将新的区域滚动到视图中时发生了数据错误。A data error occurred when scrolling a new region into view. 该值表示以编程方式或使用滚动条将包含数据错误的单元格滚动到了视图中。This value indicates that a cell with data errors scrolled into view programmatically or with the scroll bar.


下面的代码示例演示如何使用此类型。The following code example illustrates the use of this type. 此示例摘自一个更大的示例中提供如何:处理错误在 Windows 中的数据输入时发生的窗体 DataGridView 控件This example is part of a larger example available in How to: Handle Errors That Occur During Data Entry in the Windows Forms DataGridView Control.

private void dataGridView1_DataError(object sender,
    DataGridViewDataErrorEventArgs e)
    // If the data source raises an exception when a cell value is 
    // commited, display an error message.
    if (e.Exception != null &&
        e.Context == DataGridViewDataErrorContexts.Commit)
        MessageBox.Show("CustomerID value must be unique.");
Private Sub dataGridView1_DataError(ByVal sender As Object, _
    ByVal e As DataGridViewDataErrorEventArgs) _
    Handles dataGridView1.DataError

    ' If the data source raises an exception when a cell value is 
    ' commited, display an error message.
    If e.Exception IsNot Nothing AndAlso _
        e.Context = DataGridViewDataErrorContexts.Commit Then

        MessageBox.Show("CustomerID value must be unique.")

    End If

End Sub


此枚举的成员可能会使用位或运算来表示数据绑定的状态组合DataGridView在发生数据错误时。Members of this enumeration may be combined using the bitwise OR operation to represent the state of a data-bound DataGridView when a data error occurred. 例如,如果用户输入无效的单元格值 (例如,在需要日期的单元格中输入的名称),然后选择不同的单元格,DataGridView将尝试提交无效的单元格的值。For example, if a user enters an invalid cell value (such as entering a name in a cell that expects a date) and then selects a different cell, the DataGridView will try to commit the invalid cell value. 如果提交失败,DataGridView将引发DataError事件其Context属性将具有的提交和 CurrentCellChange 值。When the commit fails, the DataGridView will raise a DataError event whose Context property will have a value of Commit and CurrentCellChange.