我在DataGridView1_CellValidating中遇到问题。每当我触发搜索按钮并将数据放在指定的行中时,它会弹出我的消息框三次。为什么我使用在datagridview中验证的单元格,因为我用它来将我的数据插入到数据库中,并且我想在每次搜索时也使用该datagridview来显示数据。当我触发搜索按钮时,是否可以暂时禁用datagridview_cellvalidating?然后再次启用,以便我可以再次使用它来输入用户的内容。这是我在DataGridView1_CellValidating中的代码:
Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
If String.IsNullOrEmpty(e.FormattedValue) Then
' Show the user a message
MessageBox.Show("You have left the cell empty")
' Fail validation (prevent them from leaving the cell)
e.Cancel = False
End If
这是我的搜索按钮
Dim conn As New MySqlConnection
conn.ConnectionString = ("server=127.0.0.1;user id=root;password=12345;database=dbsis3bkenth;")
Try
conn.Open()
sql = "SELECT LName,FName,MI FROM tblsisterbrother where IDNoBrodSis = '" & cbIDNo.Text & "'"
cmd = New MySqlCommand(sql, conn)
dr = cmd.ExecuteReader
dr.Read()
If dr.HasRows = True Then
MessageBox.Show("Record Found.!")
Else
MessageBox.Show("Record Unfound.!")
End If
dr.Close()
Catch ex As MySqlException
MessageBox.Show("Error in searching to database:error is:" & ex.Message)
Exit Sub
End Try
dr.Close()
Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter
DataAdapter1.SelectCommand = cmd
DataAdapter1.Fill(ds, "tblsisterbrother")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "tblsisterbrother"
conn.Dispose()
conn.Close()
End Sub
请帮帮我并提出建议。谢谢:))
答案 0 :(得分:0)
您将删除订阅的活动,然后在完成工作后重新订阅
RemoveHandler DataGridView1.CellValidating, AddressOf DataGridView1_CellValidating
' do work
AddHandler DataGridView1.CellValidating, AddressOf DataGridView1_CellValidating