[英]how to insert data from datagridview to database mysql vb.net
[英]How to disable Datagridview1_cellvalidating temporarily in vb.net when querying data from mysql
我在DataGridView1_CellValidating中遇到问题。 每次我按下搜索按钮,并且将数据放置在指定行中时,它都会弹出我的消息框三次。 为什么我在该datagridview中使用一个单元格进行验证,因为我将其用于将数据插入数据库中,并且我想每次使用Search时也要使用该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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.