I would like to validate that a DataGridView column only accepts integers.
I used the Keypress
event, but this event was not fired on pressing a key.
Here's the code I used
private void dgvSaleReturnWintoutInvoice_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -= new KeyPressEventHandler(dgvSaleReturnWintoutInvoice_KeyPress);
if (dgvSaleReturnWintoutInvoice.Columns["dgvReturnQTY"].Index == dgvReturnQTY.Index)
{
TextBox tbt = e.Control as TextBox;
if (tbt != null)
{
tbt.KeyPress += new KeyPressEventHandler(dgvSaleReturnWintoutInvoice_KeyPress);
}
}
}
private void dgvSaleReturnWintoutInvoice_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}