首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

datagridview数据验证的有关问题

2012-03-24 
datagridview数据验证的问题我在DataGridView1_CellValidating事件里用DataGridView1.Rows(e.RowIndex).Er

datagridview数据验证的问题
我在     DataGridView1_CellValidating   事件里   用     DataGridView1.Rows(e.RowIndex).ErrorText   提示错误信息没有问题,但是我把它改成     DataGridView1.Rows(e.RowIndex).Cells   (e.ColumnIndex   ).ErrorText     让错误信息显示在单元格里的时候,却不正常,验证出错误的时候不显示错误信息,把数据改好之后错误信息才显示出来,不知道是为什么???
  Private   Sub   DataGridView1_CellValidating(ByVal   sender   As   Object,   _
        ByVal   e   As   System.Windows.Forms.DataGridViewCellValidatingEventArgs)   Handles   DataGridView1.CellValidating
                If   DataGridView1.Columns(e.ColumnIndex).Name   =   "company "   Then
                        If   String.IsNullOrEmpty(e.FormattedValue.ToString())   Then
                                DataGridView1.Rows(e.RowIndex).ErrorText   =   _
                                        "Company   Name   must   not   be   empty "
                                e.Cancel   =   True
                        End   If
                End   If
        End   Sub

[解决办法]
Private Sub DataGridView1_CellValidating(ByVal sender As Object, _
 ByVal e As indows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating 
If DataGridView1.Columns(e.ColumnIndex).Name = "company" Then 
If String.IsNullOrEmpty(e.FormattedValue.ToString()) Then 
DataGridView1.Rows(e.RowIndex).ErrorText = _ 
"Company Name must not be empty" 
e.Cancel = True <-----请将这里修改为 e.Cancel=False
End If 
Else
Dim C as DataGridViewCell=Me.DataGridView1.Row(e.RowIndex).Cells(e.ColumnIndex)
If Not(C.ErrorText=String.Empty)
C.ErrorText=String.Empty
End If
End If 
End Sub
[解决办法]
约束在dataTable来做

NotNull

热点排行