Is there any way to customize datagridview column to accept only numeric values. Also if user press any other character other than numbers nothing must type on the current cell.Is there any way to solve this problem
Asked
Active
Viewed 3.1k times
4
-
regular expression can be your close friend... – Nudier Mena Jun 10 '13 at 06:22
-
Look for "javascript filter input", for example here: http://stackoverflow.com/questions/469357/html-text-input-allow-only-numeric-input – Uriil Jun 10 '13 at 06:23
-
I divide your question in two parts for simplicity: 1- Is there any way to customize datagridview column to accept only numeric values: I think it totally depends on your gridview datasource . May be there is some way but need to google this. 2- if user press any other character other than numbers nothing must type on the current cell : Yes ofcorse we can use JavaScript function to allow the user to enter only numeric values. – garvit gupta Jun 10 '13 at 06:25
-
I guess this is winform so you could try handling the keypress event for the editing control of that particular column [similar to this](http://stackoverflow.com/questions/8321871/how-to-make-a-textbox-accept-only-alphabets/8321942#8321942) – V4Vendetta Jun 10 '13 at 06:28
-
@V4Vendetta I think you gave me a nice clue I will follow it and see what happen – user2431035 Jun 10 '13 at 06:33
-
Use a regular expression. Or if its being enterd into a database set the database type to INT – ErikMes Jun 10 '13 at 06:52
4 Answers
9
private void gvAppSummary_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (gvAppSummary.CurrentCell.ColumnIndex == intRate)
{
e.Control.KeyPress += new KeyPressEventHandler(gvAppSummary_KeyPress);
}
}
private void gvAppSummary_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
}

WhySoSerious
- 1,930
- 18
- 18

RekhaShanmugam
- 101
- 2
- 5
-
1Hint: 'intRate' is the ColumnIndex of the column that should only accept numeric values. Therefore this solution is a bit more beautiful than matzone's – Anonymous Apr 03 '14 at 06:25
4
With the previous solutions, every time you enter the EditingControlShowing event, you will add the KeyPressEvent in «the list» of events to perform on KeyPress. This can easily be checked by setting a breakpoint in the KeyPress event.
Better solution would be:
private static KeyPressEventHandler NumericCheckHandler = new KeyPressEventHandler(NumericCheck);
private void dataGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGrid.CurrentCell.ColumnIndex == numericColumn.Index)
{
e.Control.KeyPress -= NumericCheckHandler;
e.Control.KeyPress += NumericCheckHandler;
}
}
And the Event NumericCheck:
private static void NumericCheck(object sender, KeyPressEventArgs e)
{
DataGridViewTextBoxEditingControl s = sender as DataGridViewTextBoxEditingControl;
if (s != null && (e.KeyChar == '.' || e.KeyChar == ','))
{
e.KeyChar = System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
e.Handled = s.Text.Contains(e.KeyChar);
}
else
e.Handled = !char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar);
}
3
Use datagridview Editingcontrolshowing .. Basicly like this
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
String sCellName = dataGridView1.Columns(e.ColumnIndex).Name;
If (UCase(sCellName) == "QUANTITY") //----change with yours
{
e.Control.KeyPress += new KeyPressEventHandler(CheckKey);
}
}
private void CheckKey(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
}
You can improve this CheckKey ...

matzone
- 5,703
- 3
- 17
- 20
-
not work error in calling event fix that e.Control.KeyPress += new KeyPressEventHandler(CheckKey); – user2491383 Mar 18 '14 at 09:15
-
I know it's a few years late, but it's always good practice to unhook your event handler so you don't end up with tons of subscribers, e.g e.Control.KeyPress -= CheckKey before subscribing. – Tom Johnson May 28 '20 at 07:52
0
e.Control.KeyPress -= new KeyPressEventHandler(Column18qty_KeyPress);
if (dgvProduct.CurrentCell.ColumnIndex == 18) //dgvtxtQty
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.KeyPress += new KeyPressEventHandler(Column18qty_KeyPress);
}
}
private void Column18qty_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
}

user3614548
- 1
- 1