DataGridViewDataErrorContexts 枚举


表示进行了数据绑定的 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 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 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 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 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 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 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 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 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 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 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 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属性的值将为 commit 和 CurrentCellChange。When the commit fails, the DataGridView will raise a DataError event whose Context property will have a value of Commit and CurrentCellChange.