VB.NET

Moderators: seancampbell
Number of threads: 4020
Number of posts: 10026

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Validating numeric data in datagrid Posted by achalmehra on 16 Jan 2011 at 12:55 AM
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

Report
Re: Validating numeric data in datagrid Posted by seancampbell on 21 Jan 2011 at 2:50 PM
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.

    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


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

Hope that helps :)
Sean Campbell - FireSickle



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.