Validating numeric data in datagrid - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Validating numeric data in datagrid

achalmehraachalmehra Posts: 1Member
Hi Friends,
I am new to vb.net.Presently i am using VS 2010,SQL server 2008 express R2 and developing a windows application in vb.net

Problem :
I one form i am using datagrid with 3 columns.
1 col : databound ,type combobox
2 col : text bx
3 col : text box

I want user to choose value from combobox and enter only numeric value in 2nd and 3rd column.I am writing following code :

Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles Dg_bill.CellValidating

If Dg_bill.CurrentCell.ColumnIndex = 1 Then
If Not IsNumeric(Dg_bill.Item(1, 0).Value) Then
MessageBox.Show("error aa gia je")
End If
End If
End Sub


but the error msg is displayed for all values even when i enter numeric value like '1'.Plz help also in debugging i found Dg_bill.Item(1, 0).Value contains nothing alwyz

Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    Hey there, found you a solution directly on the MSDN! How lucky :).

    I modified their code ever so slightly to fit your code, but I added the link to the article for your sake.

    [code]
    Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles Dg_bill.CellValidating
    Dim newInteger As Integer

    ' Don't try to validate the 'new row' until finished
    ' editing since there
    ' is not any point in validating its initial value.

    'This checks if the current row is a new row and calls RETURN to exit the sub
    If Dg_bill.Rows(e.RowIndex).IsNewRow Then Return

    If Not Integer.TryParse(e.FormattedValue.ToString(), newInteger) Then
    e.Cancel = True
    MessageBox.Show("error aa gia je" & " at row:" & e.RowIndex.ToString())

    'Me.DataGridView1.Rows(e.RowIndex).ErrorText = "the value must be an integer"
    End If
    End Sub
    [/code]

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvalidating.aspx

    Hope that helps :)
    Sean Campbell - FireSickle
Sign In or Register to comment.