[RESOLVED] DataTable and GridView - Validate when user enters something
Hi guys :wave:
I'm playing with XML, DataGridView control, DataSet and DataTable.
Here's my code:
vb.net Code:
Imports System.Xml
Imports System.Data
Public Class Form1
Dim dt As DataTable
Dim ds As New DataSet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dt = New DataTable("product")
dt.Columns.Add(New DataColumn("Product_ID", Type.GetType("System.Int32")))
dt.Columns.Add(New DataColumn("Product_Name", Type.GetType("System.String")))
dt.Columns.Add(New DataColumn("product_Price", Type.GetType("System.Int32")))
fillRows(1, "product1", 1111)
fillRows(2, "product2", 2222)
fillRows(3, "product3", 3333)
fillRows(4, "product4", 4444)
ds.Tables.Add(dt)
ds.DataSetName = "company"
DataGridView1.DataSource = dt
End Sub
'~~~ To fill sample rows
Private Sub fillRows(ByVal pID As Integer, ByVal pName As String, ByVal pPrice As Integer)
Dim dr As DataRow
dr = dt.NewRow()
dr("Product_ID") = pID
dr("Product_Name") = pName
dr("product_Price") = pPrice
dt.Rows.Add(dr)
End Sub
'~~~ Save as XML
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ds.WriteXml("c:\Product.xml")
MsgBox("Done")
End Sub
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 :wave:
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:
Imports System.Xml
Imports System.Data
Public Class Form1
Dim dt As DataTable
Dim ds As New DataSet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dt = New DataTable("product")
dt.Columns.Add(New DataColumn("Product_ID", Type.GetType("System.Int32")))
dt.Columns.Add(New DataColumn("Product_Name", Type.GetType("System.String")))
dt.Columns.Add(New DataColumn("product_Price", Type.GetType("System.Int32")))
fillRows(1, "product1", 1111)
fillRows(2, "product2", 2222)
fillRows(3, "product3", 3333)
fillRows(4, "product4", 4444)
ds.Tables.Add(dt)
ds.DataSetName = "company"
DataGridView1.DataSource = dt
End Sub
'~~~ To fill sample rows
Private Sub fillRows(ByVal pID As Integer, ByVal pName As String, ByVal pPrice As Integer)
Dim dr As DataRow
dr = dt.NewRow()
dr("Product_ID") = pID
dr("Product_Name") = pName
dr("product_Price") = pPrice
dt.Rows.Add(dr)
End Sub
'~~~ Save as XML
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ds.WriteXml("c:\Product.xml")
MsgBox("Done")
End Sub
'~~~ Validate
Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
Select Case DataGridView1.Columns(e.ColumnIndex).Name
Case "Product_Name", "Product_ID", "product_Price"
If String.IsNullOrEmpty(e.FormattedValue.ToString()) Then
DataGridView1.Rows(e.RowIndex).ErrorText = "must not be empty"
e.Cancel = True
End If
End Select
End Sub
'~~~ Remove error msgs, if any
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
DataGridView1.Rows(e.RowIndex).ErrorText = String.Empty
End Sub
End Class
:wave:
Re: DataTable and GridView - Validate when user enters something
numeric values...
vb Code:
Private Sub DataGridView2_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If e.ColumnIndex = 0 OrElse e.ColumnIndex = 2 Then
If Not Integer.TryParse(e.FormattedValue.ToString(), 0) Then
e.Cancel = True
Me.DataGridView1.Rows(e.RowIndex).ErrorText = "need numeric values"
Else
Me.DataGridView1.Rows(e.RowIndex).ErrorText = String.Empty
End If
End If
End Sub
Re: DataTable and GridView - Validate when user enters something