0

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;
            }
        }
Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
Micro M
  • 1
  • 1
  • 4
  • I'm guessing `tbt.KeyPress +=..` is never executed because `tbt` is not a `TextBox`. Stepping though your code should reveal that easily. – Geert Bellekens Dec 29 '20 at 11:01
  • @Geert The type of the control that does text editing for a DGV is a System.Windows.Forms.DataGridViewTextBoxEditingControl; an immediate descendant of TextBox, so it should be OK – Caius Jard Dec 29 '20 at 11:14
  • 1
    @CaiusJard OK. The main point I was trying to make was that debugging the code by stepping though it should reveal the real problem immediately. – Geert Bellekens Dec 29 '20 at 11:16
  • Thank you man, Code is correct, There was a problem of column indexing, Now while debuging the code i got the point – Micro M Dec 29 '20 at 11:23
  • Read the Remarks section of [DataGridView.EditingControlShowing](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridview.editingcontrolshowing): as of now, you're adding the event handler multiple times. It's also not actually needed. If you want to handle this kind of filter, build a custom `DataGridViewTextBoxColumn` and define its editing control's features. Since the EditingControl is actually a TextBox, you can also add `ES_NUMBER` to its Styles, so the TextBox will accept numbers only (it also affects pasted text). – Jimi Dec 29 '20 at 11:31
  • I think you can take help from this question. [https://stackoverflow.com/questions/17017864/only-allow-to-enter-numeric-values-datagridview-specific-column](https://stackoverflow.com/questions/17017864/only-allow-to-enter-numeric-values-datagridview-specific-column) – Bilal Bin Zia Dec 30 '20 at 11:24

1 Answers1

1

Your code works for me, though the way I detected the current column differs:

if (someDataGridView.CurrentCell.OwningColumn == someDataGridView.Columns["someColumn"])

I would also point out that your code doesn't prevent someone pasting alpha text into the box

Caius Jard
  • 72,509
  • 5
  • 49
  • 80