Datagridview data verification cellvalidating ()

Source: Internet
Author: User
For Textbox, you can use the keypress () event to Prevent Users From inputting invalid data.
The cell in the datagridview cannot be controlled through keypress (). Maybe I have not found a method.
The cellvalidating () method is used to determine when the user finishes editing. If the data is not properly restored.
Private Void Datagridview1_cellvalidating ( Object Sender, datagridviewcellvalidatingeventargs E)
{
// Editable column
If (E. columnindex! = 2 & E. columnindex! = 3 )
Return ;
Double Outdb = 0 ;
If ( Double . Tryparse (E. formattedvalue. tostring (), Out Outdb ))
{
E. Cancel = False ;
}
Else
{
E. Cancel = True ; // Restore if the data format is incorrect.
Datagridview1.canceledit ();
}
}

Keypress () event of textbox Private Void Txtk_keypress ( Object Sender, keypresseventargs E)
{
Double Outdb = 0 ;
If ( Double . Tryparse (txtk. Text + E. keychar. tostring (), Out Outdb ))
{
E. Handled = False ;
}
Else
{
E. Handled = True ;
}
}

I inherited textbox override keypress () and encapsulated a custom control.
URL: Success
Requirements:
Focus is not left after verification error.
Implementation:
You can use the dgv_details_cellvalidating event for cell verification.
When verification fails, call E. Cancel = true. When the event chain is terminated, the cell remains editable.
Call dgv_details.canceledit (); the cell content can be rolled to the value before modification.
Use System. Windows. Forms. sendkeys. Send ("^ A"); To select all cells.

Select and edit the cell status
Implementation:
// Obtain the focus of the datagridview.
Dgv_details.focus ();
// Specify the current cell in the datagridview.
Dgv_details.currentcell = dgv_details [0, 0];
// Start editing status
Dgv_details.beginedit (false );

Custom Auto generate bound Columns
Implementation:
Dgv_details.autogeneratecolumns = false;

Set the column background color
Implementation:
Color gridreadonlycolor = color. lightgoldenrodyellow;
Dgv_details.columns [1]. defaultcellstyle. backcolor =
Winkeys. gridreadonlycolor;

Design of cell verification for the datagridview
Question 1: Are you bound or not?
Binding advantages: relatively simple, Code Less.
Disadvantages of binding: Data in the datagridview is affected by the data source (primary key constraint and Value Type constraint ). When the input content is different, the dataerror event is triggered, and the input content cannot be saved to cells and data sources. For special verification (such as length and format), you still need to write code for implementation.
About adding rows. There is a problem with the Multi-primary key verification when a new row is added, and when the verification fails, all new rows will be deleted. There are many restrictions, which is inconvenient.

Advantage of non-binding: flexible verification and other processing. Data sources are not restricted.
Non-binding disadvantages: it requires a lot of code implementation to display and update data to the database, and the efficiency is relatively low.

In general, I prefer the non-binding mode when it is difficult to handle verification. If the data volume is large, it is better to use the binding mode when the verification is relatively simple.

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.