国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

用 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技術!
  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 格尔木市| 巍山| 屯留县| 菏泽市| 沙坪坝区| 新蔡县| 托里县| 蓬安县| 大港区| 太保市| 赣州市| 宁化县| 霍山县| 册亨县| 湖南省| 阿尔山市| 曲周县| 百色市| 桂阳县| 竹北市| 友谊县| 陇南市| 仁怀市| 外汇| 绥芬河市| 玛纳斯县| 岐山县| 高台县| 凌源市| 潮安县| 定安县| 齐河县| 建阳市| 克山县| 马公市| 梁山县| 平远县| 绵竹市| 浦县| 长武县| 永川市|