用 Windows 窗体 DataGrid 控件验证输入

datagrid|window|控件

Windows 窗体 DataGrid 控件验证输入
Windows 窗体 DataGrid 控件有两种可用的输入验证类型。如果用户试图输入一个值,而该值具有单元格不可接受的数据类型(例如,向需要整数的单元格中输入一个字符串),则新的无效值将替换为旧值。这种输入验证是自动完成的,不能进行自定义。

另一种的输入验证可用于拒绝任何不可接受的数据,例如,在必须大于或等于 1 的字段中输入 0,或者一个不合适的字符串。这是在数据集中通过编写 DataTable.ColumnChanging 或 DataTable.RowChanging 事件的事件处理程序来完成的。以下示例使用 ColumnChanging 事件,因为“Product”列特别不允许不可接受的值。您可以使用 RowChanging 事件来检查“End Date”列的值是否晚于同一行中“Start Date”的值。

验证用户输入

1.               编写代码以处理相应表的 ColumnChanging 事件。当检测到不适当的输入时,调用 DataRow 对象的 SetColumnError 方法。

2.                    ' Visual Basic

3.                    Private Sub Customers_ColumnChanging(ByVal sender As Object, _

4.                    ByVal e As System.Data.DataColumnChangeEventArgs)

5.                       ' Only check for errors in the Product column

6.                       If (e.Column.ColumnName.Equals("Product")) Then

7.                          ' Do not allow "Automobile" as a product.

8.                          If CType(e.ProposedValue, String) = "Automobile" Then

9.                             Dim badValue As Object = e.ProposedValue

10.                         e.ProposedValue = "Bad Data"

11.                         e.Row.RowError = "The Product column contians an error"

12.                         e.Row.SetColumnError(e.Column, "Product cannot be " & _

13.                         CType(badValue, String))

14.                      End If

15.                   End If

16.                End Sub

17.                 

18.                // C#

19.                //Handle column changing events on the Customers table

20.                private void Customers_ColumnChanging(object sender, System.Data.DataColumnChangeEventArgs e) {

21.                 

22.                   //Only check for errors in the Product column

23.                   if (e.Column.ColumnName.Equals("Product")) {

24.                 

25.                      //Do not allow "Automobile" as a product

26.                      if (e.ProposedValue.Equals("Automobile")) {

27.                         object badValue = e.ProposedValue;

28.                         e.ProposedValue = "Bad Data";

29.                         e.Row.RowError = "The Product column contains an error";

30.                         e.Row.SetColumnError(e.Column, "Product cannot be " + badValue);

31.                      }

32.                   }

}

33.           将事件处理程序连接到事件。

将以下代码置于窗体的 Load 事件或其构造函数内。

' Visual Basic

' Assumes the grid is bound to a dataset called customersDataSet1

' with a table called Customers.

' Put this code in the form's Load event or its constructor.

AddHandler customersDataSet1.Tables("Customers").ColumnChanging, AddressOf Customers_ColumnChanging

 

// C#

// Assumes the grid is bound to a dataset called customersDataSet1

// with a table called Customers.

// Put this code in the form's Load event or its constructor.

customersDataSet1.Tables["Customers"].ColumnChanging += new DataColumnChangeEventHandler(this.Customers_ColumnChanging);

时间: 2024-05-23 01:18:45

用 Windows 窗体 DataGrid 控件验证输入的相关文章

在C#窗体程序中,如何判断textbox控件中输入的格式是浮点型

问题描述 在C#窗体程序中,如何判断textbox控件中输入的格式是浮点型?求教,谢谢咯! 解决方案 解决方案二:简单点try{float.Parse();}catch{//不是}解决方案三:1L方法最简单,还可以用正则判断解决方案四:Regex.IsMatch(strValue,@"^d+.d+$")解决方案五:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSyst

窗体编程 c# datagrid-窗体编程中的DataGrid控件的使用

问题描述 窗体编程中的DataGrid控件的使用 我想用c#实现一个功能,就是点击DataGrid控件中的某一行,这行记录中的内容就显示到相应的textBox中,比如一行记录中包括姓名,年龄,性别三列,点击后姓名显示在textBox1中,年龄显示在textBox2中,性别显示在textBox3中. 应该怎么实现?我做的是窗体的程序.求具体代码~谢谢各位了.

ASP.NET:DataGrid控件的分页功能

asp.net|datagrid|datagrid控件|分页 DataGrid 控件是一个基于模板样式的.多功能的.多列的数据绑定控件.在三个数据绑定控件中,DataGrid是功能最为强大的,其次时DataList控件,然后是轻量级的数据绑定控件Repeater.要自定义 DataGrid 中各列的布局,您可以将列类型设置为"模板",然后修改列的模板.DataGrid 控件也可以无需模板,只需按照默认的设置即可进行呈现,这使得该控件成了用于报告方案的理想控件.      我们可以把Da

vb的datagrid控件的使用(二)

创建 Northwind 的OLE DB 数据链接访问数据的一个重要步骤是为想要访问的每个数据库都创建一个 OLE DB数据源.下面的步骤为 Visual Basic 所提供的Nwind.mdb (Northwind) 数据库创建这样一个对象.这个数据源被用于 Visual Basic 文档所提供的一些示例过程.在一个计算机上只需要创建一次OLE DB数据源. 要创建 Northwind 的OLE DB 数据源1. 打开 Windows Explorer或 Windows NT Explorer

ASP.NET:DataGrid控件的排序功能

asp.net|datagrid|datagrid控件|排序 上一节我们已经知道DataGrid排序功能是由AllowSorting属性控制的,这一小节里,我们将通过实例来验证这个功能.     在DataCon Web项目里,添加一个窗体,命名为DataGrid_Sample4.aspx,添加一个DataGrid控件,DataGrid_Sample4.aspx的主要HTML代码如下:<body MS_POSITIONING="GridLayout"><form id

暂时性的解决datagrid控件数据绑定时候xxx字段不属于xxx表的错误

datagrid|datagrid控件|错误|定时|解决|数据   using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Data;using System.Windows.Forms; namespace Device.Forms.UI.Grids{ /// <summary> /// NewDataGrid 的摘要说明. /// </sum

vb的datagrid控件的使用(一)

使用 DataGrid 控件 DataGrid 控件是一种类似于电子数据表的绑定控件,可以显示一系列行和列来表示 Recordset 对象的记录和字段.可以使用 DataGrid 来创建一个允许最终用户阅读和写入到绝大多数数据库的应用程序.DataGrid 控件可以在设计时快速进行配置,只需少量代码或无需代码.当在设计时设置了DataGrid 控件的 DataSource 属性后,就会用数据源的记录集来自动填充该控件,以及自动设置该控件的列标头.然后您就可以编辑该网格的列:删除.重新安排.添加列

vb的datagrid控件的使用(四)

使用 Bookmarks 和 SelBookmarks 跟踪记录Bookmarks 和 SelBookmarks 提供了标记记录的一种手段.当编写应用程序中的特定功能(诸如允许最终用户手工地选择多个不相邻的记录,进行所选记录的大批更新)时,这就很有必要.在这些情形中,需要标记哪些记录已被选择,因此可以使用 SelBookmarks 集合及其属性.有两个函数,分别是 CellText 和 CellValue 方法,需要标记才能正确执行. 标记用户的选择SelBookmarks 集合包含所有选定的记

ASP.NET:DataGrid控件的编辑功能

asp.net|datagrid|datagrid控件 在ASP技术作Web编程的时候,因为对数据库的操作使用的RecordSet对象,如果不使用第三方控件,想要做到在线编辑数据就很困难.而DataGrid控件就支持了在线编辑的功能,只要把EditCommandColumn属性设置适当,稍加编程就可以实现了. DataGrid控件的EditItemIndex属性表示编辑按钮的类别,ASP.NET默认的EditItemIndex=-1,即不支持编辑属性.下面我们通过实例来学习一下.     在Da