DataGridViewDataErrorContexts 枚举
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示发生数据错误时数据绑定 DataGridView 控件的状态。
此枚举支持其成员值的按位组合。
public enum class DataGridViewDataErrorContexts
[System.Flags]
public enum DataGridViewDataErrorContexts
[<System.Flags>]
type DataGridViewDataErrorContexts =
Public Enum DataGridViewDataErrorContexts
- 继承
- 属性
字段
| 名称 | 值 | 说明 |
|---|---|---|
| Formatting | 1 | 尝试格式化要发送到数据存储或从数据存储加载的数据时发生数据错误。 此值指示对单元格的更改无法正确设置格式。 需要更正新单元格值,或者需要更改单元格的格式。 |
| Display | 2 | 显示由数据源填充的单元格时发生数据错误。 此值指示数据源中的值不能由单元格显示,或者将数据源中的值转换为单元格的映射缺失。 |
| PreferredSize | 4 | 计算单元格的首选大小时发生数据错误。 此值指示 DataGridView 在以编程方式调整列或行大小时无法计算单元格的首选宽度或高度。 如果单元格无法设置其值的格式,则可能会出现这种情况。 |
| RowDeletion | 8 | 删除行时发生数据错误。 此值指示当数据绑定 DataGridView 删除行时,基础数据存储会引发异常。 |
| Parsing | 256 | 分析新数据时发生数据错误。 此值指示 DataGridView 无法分析用户输入或从基础数据存储加载的新数据。 |
| Commit | 512 | 向数据存储提交更改时发生数据错误。 此值指示在单元格中输入的数据无法提交到基础数据存储。 |
| InitialValueRestoration | 1024 | 将数据还原到其以前的值时发生数据错误。 此值指示单元格尝试取消编辑,并且回滚到初始值失败。 如果单元格格式已更改,使其与初始值不兼容,则可能会出现这种情况。 |
| LeaveControl | 2048 | 丢失焦点 DataGridView 时发生数据错误。 此值指示 DataGridView 在失去焦点后无法提交用户更改。 |
| CurrentCellChange | 4096 | 当选择光标移动到另一个单元格时发生数据错误。 此值指示用户在以前选择的单元格有错误条件时选择了单元格。 |
| Scroll | 8192 | 将新区域滚动到视图中时发生数据错误。 此值指示数据错误的单元格以编程方式或滚动条滚动到视图中。 |
| ClipboardContent | 16384 | 将数据复制到剪贴板时发生数据错误。 此值指示无法将单元格值转换为字符串。 |
示例
下面的代码示例演示了此类型的用法。 本示例是 “操作方法:处理 Windows 窗体 DataGridView 控件中数据输入期间发生的错误”的较大示例的一部分。
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
注解
可以使用按位 OR 操作合并此枚举的成员,以表示发生数据错误时数据绑定 DataGridView 的状态。 例如,如果用户输入无效的单元格值(例如在需要日期的单元格中输入名称),然后选择其他单元格, DataGridView 则将尝试提交无效的单元格值。 当提交失败时,将 DataGridView 引发一个 DataError 事件,该 Context 事件的属性将具有 Commit 和 CurrentCellChange 的值。