DataGridView cell Input limit for WinForm

Source: Internet
Author: User

Add code to the DataGridView editingcontrolshowing event:

if (this.dgv_pch. Columns[dgv_pch. Currentcell.columnindex]. HeaderText = = "Batch number")//Determine which column of the cell needs to be limited
{
(dgv_pch. Columns[dgv_pch. Currentcell.columnindex] as Datagridviewtextboxcolumn). Maxinputlength = 4;//Limit only input content length is 4
e.Control.KeyPress + = new Keypresseventhandler (editingcontrol_keypress);//limit only enter numbers
e.Control.KeyPress-= new Keypresseventhandler (editingcontrol_keypress);
}

private void Editingcontrol_keypress (object sender, KeyPressEventArgs e)
{
if (Convert.ToInt32 (e.keychar) = = 8)
{
e.handled = false;
}
Else
{
Regex Numregex = new Regex (@ "^ (-?[ 0-9]) $ "); Match Regular expression
Match result = Numregex.match (convert.tostring (E.keychar));
if (result. Success)//input is not a number
{
e.handled = false; The textbox content does not change
}
Else
{
E.handled = true; Save the value of the textbox now
}
}
}

DataGridView cell Input limit for WinForm

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.