Results 1 to 4 of 4

Thread: [RESOLVED] DataTable and GridView - Validate when user enters something

  1. #1

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Resolved [RESOLVED] DataTable and GridView - Validate when user enters something

    Hi guys

    I'm playing with XML, DataGridView control, DataSet and DataTable.
    Here's my code:
    vb.net Code:
    1. Imports System.Xml
    2. Imports System.Data
    3.  
    4. Public Class Form1
    5.     Dim dt As DataTable
    6.     Dim ds As New DataSet
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.  
    10.         dt = New DataTable("product")
    11.         dt.Columns.Add(New DataColumn("Product_ID", Type.GetType("System.Int32")))
    12.         dt.Columns.Add(New DataColumn("Product_Name", Type.GetType("System.String")))
    13.         dt.Columns.Add(New DataColumn("product_Price", Type.GetType("System.Int32")))
    14.         fillRows(1, "product1", 1111)
    15.         fillRows(2, "product2", 2222)
    16.         fillRows(3, "product3", 3333)
    17.         fillRows(4, "product4", 4444)
    18.         ds.Tables.Add(dt)
    19.  
    20.         ds.DataSetName = "company"
    21.  
    22.         DataGridView1.DataSource = dt
    23.        
    24.     End Sub
    25.  
    26.     '~~~ To fill sample rows
    27.     Private Sub fillRows(ByVal pID As Integer, ByVal pName As String, ByVal pPrice As Integer)
    28.         Dim dr As DataRow
    29.         dr = dt.NewRow()
    30.         dr("Product_ID") = pID
    31.         dr("Product_Name") = pName
    32.         dr("product_Price") = pPrice
    33.         dt.Rows.Add(dr)
    34.     End Sub
    35.  
    36.     '~~~ Save as XML    
    37.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    38.         ds.WriteXml("c:\Product.xml")
    39.         MsgBox("Done")
    40.     End Sub
    41.  
    42. End Class
    My question is, the user can edit/add records via DataGridView control. So, when the user enters a string value where it is expected to give an integer value(first column and third column), it would give a big error message. Instead of showing that, I would like to apply custom validations.

    How would I do it ?

    Thanks

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  2. #2

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: DataTable and GridView - Validate when user enters something

    Found a solution: http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx

    Here's the code:
    vb.net Code:
    1. Imports System.Xml
    2. Imports System.Data
    3.  
    4. Public Class Form1
    5.     Dim dt As DataTable
    6.     Dim ds As New DataSet
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.  
    10.         dt = New DataTable("product")
    11.         dt.Columns.Add(New DataColumn("Product_ID", Type.GetType("System.Int32")))
    12.         dt.Columns.Add(New DataColumn("Product_Name", Type.GetType("System.String")))
    13.         dt.Columns.Add(New DataColumn("product_Price", Type.GetType("System.Int32")))
    14.         fillRows(1, "product1", 1111)
    15.         fillRows(2, "product2", 2222)
    16.         fillRows(3, "product3", 3333)
    17.         fillRows(4, "product4", 4444)
    18.         ds.Tables.Add(dt)
    19.  
    20.         ds.DataSetName = "company"
    21.  
    22.         DataGridView1.DataSource = dt
    23.        
    24.     End Sub
    25.  
    26.     '~~~ To fill sample rows
    27.     Private Sub fillRows(ByVal pID As Integer, ByVal pName As String, ByVal pPrice As Integer)
    28.         Dim dr As DataRow
    29.         dr = dt.NewRow()
    30.         dr("Product_ID") = pID
    31.         dr("Product_Name") = pName
    32.         dr("product_Price") = pPrice
    33.         dt.Rows.Add(dr)
    34.     End Sub
    35.  
    36.     '~~~ Save as XML    
    37.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    38.         ds.WriteXml("c:\Product.xml")
    39.         MsgBox("Done")
    40.     End Sub
    41.  
    42.     '~~~ Validate
    43.     Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    44.  
    45.         Select Case DataGridView1.Columns(e.ColumnIndex).Name
    46.             Case "Product_Name", "Product_ID", "product_Price"
    47.  
    48.                 If String.IsNullOrEmpty(e.FormattedValue.ToString()) Then
    49.                     DataGridView1.Rows(e.RowIndex).ErrorText = "must not be empty"
    50.                     e.Cancel = True
    51.  
    52.                 End If
    53.  
    54.         End Select
    55.      
    56.     End Sub
    57.  
    58.     '~~~ Remove error msgs, if any
    59.     Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
    60.         DataGridView1.Rows(e.RowIndex).ErrorText = String.Empty
    61.     End Sub
    62.  
    63. End Class

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    Re: DataTable and GridView - Validate when user enters something

    numeric values...
    vb Code:
    1. Private Sub DataGridView2_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
    2.         If e.ColumnIndex = 0 OrElse e.ColumnIndex = 2 Then
    3.             If Not Integer.TryParse(e.FormattedValue.ToString(), 0) Then
    4.                 e.Cancel = True
    5.                 Me.DataGridView1.Rows(e.RowIndex).ErrorText = "need numeric values"
    6.             Else
    7.                 Me.DataGridView1.Rows(e.RowIndex).ErrorText = String.Empty
    8.             End If
    9.         End If
    10.     End Sub

  4. #4

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: DataTable and GridView - Validate when user enters something

    Thanks

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width