The datagridview Verification Cell is not empty and the primary key is unique {convert}

Source: Internet
Author: User

 

In the cellvalidating event of the datagridview datagri, verify that the input data meets your requirements. For example, if the data to be verified is empty

Private void maid (Object sender,
Datagridviewcellvalidatingeventargs E)
{
// Validate the companyName entry by disallowing empty strings.
If (maid [E. columnindex]. Name = "companyName ")
{
If (string. isnullorempty (E. formattedvalue. tostring ()))
{
Datagridview1.rows [E. rowindex]. errortext =
"Company name must not be empty ";
E. Cancel = true;
}
}
}

Void maid (Object sender, maid E)
{
// Clear the row error in case the user presses ESC.
Datagridview1.rows [E. rowindex]. errortext = string. empty;
}

The role of the above Code is
Double-clickCompanyNameYou can edit the value of a cell in the column. If you delete all characters and press tab to exit the cellDatagridviewWill prevent you from exiting. After you type a non-empty string in the cell,DatagridviewControl will allow you to exit this cell

Private void datagridview1_dataerror (Object sender,
Datagridviewdataerroreventargs E)
{
// If the data source raises an exception when a cell value is
// Commited, display an error message.
If (E. Exception! = NULL &&
E. Context = maid. Commit)
{
MessageBox. Show ("customerid value must be unique .");
}
}
The role of the above Code is

IfCustomeridEnter a duplicate value and submit the editing result. The cell value is automatically restored to the original value.MessageBox.

From: http://hi.baidu.com/sghcz_lv/blog/item/3fd9197f4202480f28388a21.html

A specific verification event can be solved. If you believe it, you can read another article:

Http://hi.baidu.com/fxb%5Fhyb/blog/item/146372237f216446925807d1.html

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.