### .NET 2.0 - WinForm Control - DataGridView 编程36 计(二) #### ① Error 图标表示的设定 在 .NET 2.0 的 WinForms 控件中,`DataGridView` 提供了一种有效的方式来帮助开发者突出显示用户输入的数据中的错误。通过设置 `ErrorText` 属性或利用 `CellErrorTextNeeded` 和 `RowErrorTextNeeded` 事件,可以方便地添加错误图标到单元格或行头。 ##### 1.1 ErrorText 属性 `ErrorText` 属性用于在单元格或行上显示错误消息,并触发错误图标的显示。该属性仅在 `DataGridView.ShowCellErrors` 设置为 `True` 时才生效,默认情况下此属性即为 `True`。 **示例代码:** ```vbnet ' 设定 (0,0) 的单元格显示 Error 图标 DataGridView1(0, 0).ErrorText = "请确认单元格的值。" ' 设定第 4 行 (Index=3) 的行头显示 Error 图标 DataGridView1.Rows(3).ErrorText = "不能输入负值。" ``` ```csharp // 设定 (0,0) 的单元格显示 Error 图标 dataGridView1[0, 0].ErrorText = "请确认单元格的值。"; // 设定第 4 行 (Index=3) 的行头显示 Error 图标 dataGridView1.Rows[3].ErrorText = "不能输入负值。"; ``` ##### 1.2 CellErrorTextNeeded 和 RowErrorTextNeeded 事件 对于更复杂的错误处理需求,可以通过 `CellErrorTextNeeded` 和 `RowErrorTextNeeded` 事件来动态设置单元格或行的错误信息。这些事件允许在运行时根据单元格或行的内容来决定是否显示错误图标及其对应的错误消息。 **示例代码:** ```vbnet ' CellErrorTextNeeded 事件处理方法 Private Sub DataGridView1_CellErrorTextNeeded(ByVal sender As Object, ByVal e As DataGridViewCellErrorTextNeededEventArgs) Handles DataGridView1.CellErrorTextNeeded Dim dgv As DataGridView = CType(sender, DataGridView) ' 单元格值为负整数时,Error 图标被显示。 Dim cellVal As Object = dgv(e.ColumnIndex, e.RowIndex).Value If TypeOf cellVal Is Integer AndAlso CInt(cellVal) < 0 Then e.ErrorText = "不能输入负整数。" End If End Sub ' RowErrorTextNeeded 事件处理方法 Private Sub DataGridView1_RowErrorTextNeeded(ByVal sender As Object, ByVal e As DataGridViewRowErrorTextNeededEventArgs) Handles DataGridView1.RowErrorTextNeeded Dim dgv As DataGridView = CType(sender, DataGridView) If dgv("Column1", e.RowIndex).Value Is DBNull.Value AndAlso _ dgv("Column2", e.RowIndex).Value Is DBNull.Value Then e.ErrorText = "Column1 和 Column2 必须输入一个值。" End If End Sub ``` ```csharp // CellErrorTextNeeded 事件处理方法 private void DataGridView1_CellErrorTextNeeded(object sender, DataGridViewCellErrorTextNeededEventArgs e) { DataGridView dgv = (DataGridView)sender; // 单元格值为负整数时,Error 图标被显示。 object cellVal = dgv[e.ColumnIndex, e.RowIndex].Value; if (cellVal is int && ((int)cellVal) < 0) { e.ErrorText = "不能输入负整数。"; } } // RowErrorTextNeeded 事件处理方法 private void DataGridView1_RowErrorTextNeeded(object sender, DataGridViewRowErrorTextNeededEventArgs e) { DataGridView dgv = (DataGridView)sender; if (dgv["Column1", e.RowIndex].Value == DBNull.Value && dgv["Column2", e.RowIndex].Value == DBNull.Value) { e.ErrorText = "Column1 和 Column2 必须输入一个值。"; } } ``` #### ② 单元格入力值得验证 除了使用错误图标来提示用户之外,还需要对用户的输入进行验证,以确保数据的有效性和准确性。这通常涉及到以下几个步骤: 1. **定义验证规则**:确定哪些字段是必需的,哪些字段需要特定格式的数据。 2. **实现验证逻辑**:编写代码来检查用户的输入是否符合定义的规则。 3. **反馈结果**:向用户提供清晰的反馈,告知他们哪些输入有问题以及如何修正。 ##### 验证规则示例 假设我们需要验证用户的年龄输入: - 年龄必须是正整数。 - 年龄不能超过150岁。 **示例代码:** ```vbnet Private Sub ValidateAge(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) If e.ColumnIndex = AgeColumnIndex And e.RowIndex >= 0 Then Dim age As Integer If Not Integer.TryParse(e.FormattedValue.ToString(), age) OrElse age <= 0 OrElse age > 150 Then e.Cancel = True e.ErrorText = "请输入有效的年龄(1-150之间)。" End If End If End Sub ``` ```csharp private void ValidateAge(object sender, DataGridViewCellValidatingEventArgs e) { if (e.ColumnIndex == AgeColumnIndex && e.RowIndex >= 0) { int age; if (!int.TryParse(e.FormattedValue.ToString(), out age) || age <= 0 || age > 150) { e.Cancel = true; e.ErrorText = "请输入有效的年龄(1-150之间)。"; } } } ``` #### ③ 用户入力值发生错误时的捕获 当用户输入的数据不符合预期时,程序需要能够有效地捕获这些错误,并采取适当的措施。通常,这包括: 1. **捕获异常**:使用 `try-catch` 块来捕获可能发生的异常。 2. **显示错误信息**:向用户展示清晰的错误消息,指导他们如何纠正错误。 3. **恢复状态**:确保应用程序的状态正确无误,防止因为错误输入而导致的程序崩溃。 **示例代码:** ```vbnet Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit Try If e.ColumnIndex = AgeColumnIndex And e.RowIndex >= 0 Then Dim age As Integer = CInt(DataGridView1(e.ColumnIndex, e.RowIndex).Value) If age <= 0 OrElse age > 150 Then Throw New ArgumentException("年龄必须在1-150之间。") End If End If Catch ex As Exception DataGridView1(e.ColumnIndex, e.RowIndex).ErrorText = ex.Message DataGridView1.EndEdit() DataGridView1.CurrentCell = DataGridView1(e.ColumnIndex, e.RowIndex) End Try End Sub ``` ```csharp private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { try { if (e.ColumnIndex == AgeColumnIndex && e.RowIndex >= 0) { int age = (int)dataGridView1[e.ColumnIndex, e.RowIndex].Value; if (age <= 0 || age > 150) { throw new ArgumentException("年龄必须在1-150之间。"); } } } catch (Exception ex) { dataGridView1[e.ColumnIndex, e.RowIndex].ErrorText = ex.Message; dataGridView1.EndEdit(); dataGridView1.CurrentCell = dataGridView1[e.ColumnIndex, e.RowIndex]; } } ``` 通过以上步骤,我们可以有效地管理和控制用户在 `DataGridView` 中的输入行为,提高数据的准确性和应用程序的健壮性。






























- 粉丝: 0
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 现有科技管理系统为何需要通过AI+数智应用实现“管理+服务”模式?.docx
- 为什么现有的科技管理系统难以满足现代需求?如何通过AI+数智应用解决?.docx
- 在科技管理新趋势下,如何通过AI+数智应用激活内外部资源,提升管理成效?.docx
- 怎样的AI+数智应用创新管理系统能真正提升科技管理效率与价值创造?.docx
- 用于目标检测技能提升的练习活动
- 基于动态窗口法与模糊控制的机器人路径规划Matlab实现及优化
- 全面解析:永磁同步电机电磁噪声与振动NVH分析全套资料教程
- AI+数智应用驱动的知识产权解决方案如何助力科技服务机构提升竞争力?.docx
- AI+数智应用科技活动组织与服务如何确保科技平台发展可持续?.docx
- AI+数智应用驱动的科技活动组织与服务怎样保障服务的有效性?.docx
- AI+数智应用驱动的智改数转服务如何帮助科技机构实现产品差异化?.docx
- 供给导向的AI+数智应用技转服务如何助力地方政府构建高效的科技创新生态?.docx
- 观点作者:科易网AI+技术转移研究院.docx
- 技术转移机构服务手段单一,如何利用AI+数智应用实现智能化转型?.docx
- 何种AI+数智应用服务能全方位助力区域科技创新体系建设?.docx
- 技术转移机构如何借助AI+数智应用突破资源对接瓶颈?.docx


