首页 > 开发 > 综合 > 正文

用 Windows 窗体 DataGrid 控件验证输入

2024-07-21 02:23:00
字体:
来源:转载
供稿:网友
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);

  • 网站运营seo文章大全
  • 提供全面的站长运营经验及seo技术!
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表