用 Windows 表單 DataGrid 控制項驗證輸入

來源:互聯網
上載者:User
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);



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。