Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I am creating a window application.
I want to enter only numeric values in the form of "999999.99" ( ie. numbers like 444.55 ) in a particular column in the DataGridView.

How can it be done?

I am working on Visual Studio 2010.

Please help.
Posted
Updated 12-Sep-22 21:14pm
v3
Comments
Zasky 26-Sep-11 6:02am    
Is the grid bound to a DataTable?
Raahul Kaushal 26-Sep-11 9:58am    
The grid is not bound to a DataTable.
Grid is used for taking the input from the user.

It's easy enough to handle OnCellValidating() to check for numeric cell input when exiting a cell, but I think you want to completely prevent non-numeric input.
That's more tricky - take a look at this article:
Custom Numeric Edit Elements for DataGridView[^]
 
Share this answer
 
use this event of datagrid for keypress...

C#
private void grd_bill_master_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        try
        {
            e.Control.KeyPress +=
  new KeyPressEventHandler(Control_KeyPress);

     
    }


// and this will allowed values like Ex. 10.20 etc...


C#
private void Control_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;
        }
    }
 
Share this answer
 
Comments
Member 14207561 23-May-19 17:32pm    
Hello, should be:

private void grd_bill_master_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
try
{
e.Control.KeyPress +=
new KeyPressEventHandler(Control_KeyPress);

((DataGridView)sender).EditingControlShowing -= new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(grd_bill_master_EditingControlShowing);

}
}
Member 15912515 5-May-23 9:45am    
Super helpful, thanks!
 
Share this answer
 
Comments
Raahul Kaushal 26-Sep-11 9:44am    
it has helped me to some extent. but i am looking for a more particular solution.
please refer to my query again.
For Windows Application You can validate value on keypress event
below is the Example.
VB
Private Sub txt_amt_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_amt.KeyPress
       Dim allowedChars As String = "0123456789." & vbBack & "\"
       If allowedChars.IndexOf(e.KeyChar) = -1 Then
           e.Handled = True
       End If
   End Sub
 
Share this answer
 
v2
Comments
Raahul Kaushal 27-Sep-11 6:40am    
The problem in validating value on KeyPress event is that this will be applied to all columns of the DataGridView.
Requirement is of only one NUMERIC Column that allows only valid numeric values as input like "432.55"
[no name] 27-Sep-11 7:06am    
Dear
This will applicable only for one column
suppose you have Aount Column in Grid. then this validation will be applicable for Amount Column.
VB
Private Sub dgvItemService_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dgvItemService.EditingControlShowing
        If TypeOf e.Control Is TextBox Then
            Dim tb As TextBox = TryCast(e.Control, TextBox)
            
            'Remove Event handler keyPress early add if any
            RemoveHandler tb.KeyPress, AddressOf tb_KeyPress

            '3=Vol, 5=UPrice, 6=Discount are the desired columns
            If Me.dgvItemService.CurrentCell.ColumnIndex = 3 _
                Or Me.dgvItemService.CurrentCell.ColumnIndex = 5 _
                Or Me.dgvItemService.CurrentCell.ColumnIndex = 6 Then 

                AddHandler tb.KeyPress, AddressOf tb_KeyPress
            End If
        End If
    End Sub

    Private Sub tb_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Not Char.IsControl(e.KeyChar) _
            And Not Char.IsDigit(e.KeyChar) _
            And e.KeyChar <> "." Then
            e.Handled = True
        End If
    End Sub
 
Share this answer
 
v3
Comments
CHill60 25-Mar-14 11:54am    
The question is over 2 years old and already has an accepted answer!!!
Sufvan Adil 2023 15-Jan-23 20:35pm    
Me.dgvItemService.CurrentCell.ColumnIndex = 3

If Me.DataGridView1.CurrentCell = DataGridView1.CurrentRow.Cells("d1qty").Value Then


System.InvalidCastException: 'Operator '=' is not defined for type 'DataGridViewTextBoxCell' and 'Nothing'.'
 
Share this answer
 
Hi Friend You Can use Ajax FilterTextBoxExtender for enter only numeric values in textbox 

C#
<cc1:filteredtextboxextender id="FilteredTextBoxExtender2"  runat="server" targetcontrolid="txtAmount" filtermode="ValidChars" filtertype="Custom" validchars="0123456789." xmlns:cc1="#unknown">

OR You can use Regular Expression for validate.
See This Link.

http://www.devcurry.com/2010/03/using-regular-expression-to-validate.html[^]
 
Share this answer
 
v3
Comments
Raahul Kaushal 26-Sep-11 9:38am    
Thats a nice solution but I am working on a window application not on a web app.
1) Add an event of EditingControlShowing.
2) In EditingControlShowing, check that if the current cell lies in the desired column.
3) Register a new event of KeyPress in EditingControlShowing(if above condition is true).
4) Remove any KeyPress event added previously in EditingControlShowing.
5) In KeyPress event, check that if key is not digit then cancel the input.

Example:
C#
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.KeyPress -= new KeyPressEventHandler(Column1_KeyPress);
            if (dataGridView1.CurrentCell.ColumnIndex == 0) //Desired Column
            {
                TextBox tb = e.Control as TextBox;
                if (tb != null)
                {
                    tb.KeyPress += new KeyPressEventHandler(Column1_KeyPress);
                }
            }
        }

private void Column1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
 
Share this answer
 
Comments
SilentBob101 25-Feb-14 4:55am    
This looks nice, but it doesn't work!
Muhammad Mobeen Qureshi 27-Feb-14 7:26am    
It works. You can verify it here too as several people have voted it up and the answer is marked too:
http://stackoverflow.com/questions/12645458/make-a-specific-column-only-accept-numeric-value-in-datagridview-in-keypress-eve
((DataGridViewTextBoxColumn)dataGridView1.Columns[1]).MaxInputLength = 59;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900