网站首页站长博客下载中心域名交易站长论坛域名主机免费电邮免费域名中文排行排名查询站长书库书籍教程下载
设为首页
加入收藏
总编信箱
投稿或申请专栏请先 [登 陆]
学院首页 网络编程 网页设计 图形图象 数 据 库 服 务 器 网络媒体 网络安全 个人专栏 站长CLUB 业界新闻 信息公告
 当前位置:首页 >> 网络编程 >> NET专区 >> 正文
公告通知
返回上级列表
资料搜索
相关文章
用户自定义控件的应用
c#.net常用函数和方法集
在VB中使用水晶报表的一种简易编
C#调用父类的父类的方法
浏览.NET Framework 2.0 类型库中
为.Text Blog 添加 计数器
编程实现邮件地址有效性检测 
VB/VB.NET/C#导出到Excel的方法
c#高性能在WEB端产生验证图片
用System.Web.Caching.Cache保存
用 Windows 窗体 DataGrid 控件验证输入
[ 来源:CSDN | 作者:未知 | 时间:2006-4-15 6:36:03 | 浏览:人次 ]
收藏到新浪ViVi 收藏到365KEY 收藏到我摘  字号选择〖    〗/ 双击滚屏 单击停止  

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);



[发送给好友]  [打印本页]  [关闭窗口]  [返回顶部]   转载请注明来源:http://edu.chinaz.com   
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
栏目编辑: 设计风 责任编辑: keke
原始作者: 未知 录入时间: 2006-4-15 6:36:03
信息来源: CSDN 投稿信箱: Edu#chinaz.com
设为首页 - 加入收藏 - 关于我们 - 广告服务 - 版权申明 - 友情链接 - 联系方式 - 总编信箱 - 会员投稿