0

Goodday 我有以下代码可以在更改行时保存更改。但对我来说重要的是如果一个单元格“描述列”为空,则不要保存更改。我怎样才能添加到这个代码,以便我得到一个消息框。“如果描述为空或为空,则无法保存”谢谢 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
4

1 回答 1

0

为 RowValidating 添加一个事件;在那里进行测试,如果失败,请将 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
于 2013-05-13T04:10:32.010 回答