I have a DataGridView. I want its 1st column or any desired column (which has textboxes in it) to be NUMERIC ONLY. I am currently using this code:
private void dataGridViewItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns["itemID"].Index)
{
TextBox itemID = e.Control as TextBox;
if (itemID != null)
{
itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
}
}
}
private void itemID_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
This code works but the problem is that all the textboxes in all the columns are getting numeric only.
I figured it out myself 🙂
Just removed the previous events in the starting of the function which resolved my issue.