Validating input with Windows forms DataGrid control

Source: Internet
Author: User
Tags constructor
datagrid|window| controls Windows Forms DataGrid Control validation input

Windows forms the DataGrid control has two types of input validation available. If a user tries to enter a value that has a data type that is not acceptable for the cell (for example, to enter a string into a cell that requires an integer), the new invalid value is replaced with the old value. This type of input validation is done automatically and cannot be customized.

Another type of input validation can be used to reject any unacceptable data, such as entering 0 in a field that must be greater than or equal to 1, or an inappropriate string. This is done in the dataset by writing an event handler for the Datatable.columnchanging or datatable.rowchanging event. The following example uses the ColumnChanging event because the "Product" column specifically does not allow unacceptable values. You can use the RowChanging event to check whether the value of the "End Date" column is later than the "Start date" value in the same row.

Validating user input

1. Write code to handle the ColumnChanging event of the corresponding table. When an improper input is detected, the SetColumnError method of the DataRow object is invoked.

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

e.ProposedValue = "Bad Data"

E.row.rowerror = "The Product column Contians an error"

E.row.setcolumnerror (E.column, "Product cannot be" & _

CType (Badvalue, String)

End If

. End If

End Sub

17.

//C #

//handle column changing events on the Customers table

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

21st.

//only Check for errors in the Product column

if (E.column.columnname.equals ("Product")) {

24.

//do not allow "automobile" as a product

if (E.proposedvalue.equals ("Automobile")) {

An. Object badvalue = e.ProposedValue;

e.ProposedValue = "Bad Data";

E.row.rowerror = "The Product column contains an error";

E.row.setcolumnerror (E.column, "Product cannot be" + badvalue);

31.}

32.}

}

33. Connect an event handler to an event.

Place the following code within the form's Load event or its constructor.

' Visual Basic

' Assumes the grid is bound to a dataset called CustomersDataSet1

' With a table called Customers.

' Put this code in the ' form ' 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);



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.