如果一个单元格为空或null,不要保存对数据视图的更改



再见,我有下面的代码来保存更改时,我改变行。但对我来说很重要的是,如果一个单元格"描述列"为空,则不要保存更改。我怎样才能添加到这个代码,以便我得到一个消息框。'如果描述为空或null,则无法保存'谢谢rob

     ' We need an indicator to know when we need to update the source database
Dim UpdatePending As Boolean = False

Private Sub ExampleBindingSource_ListChanged(ByVal sender As Object, _
    ByVal e As System.ComponentModel.ListChangedEventArgs) _
    Handles ExampleBindingSource.ListChanged
' Whenever there is an update, note that a change is pending.
'
' ListChanged does not fire when moving within a row, so this will not
' mark updates until done with the row. (Here "done" could mean moving
' to another row or closing the form.)
If Me.ExampleDataSet.HasChanges Then
    Me.UpdatePending = True
End If
End Sub
Private Sub DataGridView1_RowValidated(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
    Handles DataGridView1.RowValidated
' The RowValidated event occurs after
' BindingSource_*Changed operations, which
' makes it a good place to update our source database.
' However, this event fires at a number
' of times when we don't have pending updates.
' That's why we need the UpdatePending indicator 
' to tell us whether to do anything.
' If we have an update pending, copy it to the source database
If UpdatePending Then
    Me.ExampleTableAdapter.Update(Me.ExampleDataSet.Example)
    Me.UpdatePending = False
End If 
End Sub

为rowvalidation添加一个事件;在那里进行测试,如果失败,将e.Cancel设置为true。所以你的代码看起来像这样:

Private Sub DataGridView1_RowValidating(ByVal sender As Object, _
    ByVal e as System.Windows.Forms.DataGridViewCellEventArgs) _
    Handles DataGridView1.RowValidating
    if (String.IsNullOrEmpty(DataGridView1.CurrentCell.Value)) then
        e.Cancel = true
    end if
end sub

相关内容

  • 没有找到相关文章

最新更新