我在 DataGridView1_CellValidating 中遇到问题。每次我触发搜索按钮并将数据放置在指定的行中时,它都会弹出我的消息框 3 次。为什么我在那个 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
请帮助我并提出建议。谢谢:)
最佳答案
您将删除订阅的事件,然后在完成工作后重新订阅
RemoveHandler DataGridView1.CellValidating, AddressOf DataGridView1_CellValidating
' do work
AddHandler DataGridView1.CellValidating, AddressOf DataGridView1_CellValidating
关于mysql - 从mysql查询数据时如何在vb.net中临时禁用Datagridview1_cellvalidating,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35521008/