在vb.net中的gridview中进行验证 [英] validation in gridview in vb.net

查看:155
本文介绍了在vb.net中的gridview中进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我正在vb.net Windows应用程序中使用gridview.下面coloumn的数据类型
column1 =字符串
column2 =字符串
column3 =十进制
column4 = date

我需要在单击保存"按钮时对它们进行验证.Peels将代码发送给我.

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

推荐答案

您需要向gridview添加验证控件.
[ ^ ]示例在C#中,但是可以使您大致了解所需要做的事情.
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.


如果您将自定义类绑定到DataGridView,则可以实现 System.ComponentModel.IDataErrorInfo [
If you are binding a custom Class to your DataGridView you could Implement the System.ComponentModel.IDataErrorInfo[^] in your Class.
Implementation looks like this:
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


当它们绑定到实现IDataErrorInfo的对象时,ErrorProvider和DataGridView都将在绑定的控件和单元格/行上自动显示错误图标.然后,如果您想在做某事之前验证您的对象,则可以简单地说:


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:

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


这种方法的优点是您的验证逻辑在您的业务类中(在此示例中为Person).因此,如果您将需要重复使用Person类(在另一种形式,程序集或应用程序中),则无需重写(或更糟糕的是,复制/粘贴)验证逻辑.此人会验证自己.

希望这可以帮助! :)


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! :)


这是简单的验证过程

如果gridview.row(rowno).cells(columnname).value ="然后
msgbox
如果
This is the simple validation process

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


这篇关于在vb.net中的gridview中进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆