读书人

datagridview数据验证的有关问题

发布时间: 2012-03-24 14:00:46 作者: rapoo

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

读书人网 >VB Dotnet

热点推荐