Click here to Skip to main content
15,915,164 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
in my project i'm using gridview in vb.net windows applications. the data types for coloumn below
column1=string
column2=string
column3=decimal
column4=date

i need validations for these when clicking save button.pesels send me coding for th
Posted

You need to add validation controls to the gridview.
This[^] sample is in C#, but would give you a general idea of what you need to do.
 
Share this answer
 
Comments
Sander Rossel 16-Oct-11 8:52am    
Interesting link. Must check it out. My 5 for it.
If you are binding a custom Class to your DataGridView you could Implement the System.ComponentModel.IDataErrorInfo[^] in your Class.
Implementation looks like this:
VB
Public Class Person
	Implements System.ComponentModel.IDataErrorInfo

	Public Property FirstName As String
	Public Property LastName As String
	Public Property Age As Integer

	Public ReadOnly Property [Error] As String Implements System.ComponentModel.IDataErrorInfo.Error
		Get
			' Validation logic for the entire Object.
			' You can implement this any way you want.
			Dim errorMessage As String = String.Empty
			errorMessage &= Me.Item("FirstName") & "{0}"
			errorMessage &= Me.Item("LastName") & "{0}"
			errorMessage &= Me.Item("Age")
			Return String.Format(errorMessage, Environment.NewLine)
		End Get
	End Property

	Default Public ReadOnly Property Item(ByVal columnName As String) As String Implements System.ComponentModel.IDataErrorInfo.Item
		Get
			' Your validation logic per Property.
			' You can implement this any way you want.
			Dim errorMessage As String = String.Empty
			Select Case columnName
				Case "FirstName"
					If Me.FirstName = String.Empty Then
						errorMessage = "Firstname is a mandatory field."
					End If

				Case "LastName"
					If Me.LastName = String.Empty Then
						errorMessage = "Lastname is a mandatory field."
					End If

				Case "Age"
					If Me.Age < 18 Or Me.Age > 80 Then
						errorMessage = "Age should be a value between 18 and 80."
					End If

			End Select
			Return errorMessage
		End Get
	End Property

End Class

Both the ErrorProvider and the DataGridView automatically show error icons on bound Controls and Cells/Rows when they are bound to an Object that Implements IDataErrorInfo. Then if you want to validate your Object before doing something you could simply say:
VB
Dim p as New Person
If p.Error = String.Empty Then
   ' Do whatever you want to do here.
Else
   MessageBox.Show(String.Format("The Person is not valid.{0}{1}", Environment.NewLine, p.Error)
End If

The pro to this approach is that your validation logic is in your business Class (in this example Person). So if you would ever need to re-use the Person Class (in another Form, Assembly or Application) you do not need to rewrite (or worse, copy/paste) your validation logic. The Person validates itself.

Hope this helps! :)
 
Share this answer
 
Comments
Abhinav S 16-Oct-11 8:40am    
This could be done as well. 5.
Sander Rossel 16-Oct-11 8:52am    
Thanks! :)
This is the simple validation process

if gridview.row(rowno).cells(columnname).value = "" then
msgbox
end if
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900