|
-
Oct 5th, 2011, 06:37 AM
#1
[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:
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
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,...
-
Oct 5th, 2011, 07:10 AM
#2
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
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,...
-
Oct 5th, 2011, 07:36 AM
#3
Addicted Member
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
-
Oct 5th, 2011, 10:00 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|