我一直在为datagridview修复textChanged like事件,但是我无法得到我想要的结果。每当我更改其单元格上的文本时,dataGridView1必须过滤dataGridView2的内容。
这可以过滤我的dataGridView2的内容,但在此之前我必须单击dataGridView1 / press Tab外的光标。这是我的代码:
Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
Dim con1 As OleDbConnection = con
con1.Open()
Dim dt As New DataTable
Dim _command As OleDbCommand = New OleDbCommand()
_command.Connection = con1
_command.CommandText = "SELECT * FROM table_name WHERE " & likeContent & ""
dt.Load(_command.ExecuteReader)
Me.dgv.DataSource = dt
con1.Close()
End Sub
“likecontent”是我将文本存储在dataGridView1上的地方。
我的dataGridView2将如何通过textChanged更新,例如来自我的dataGridView1的事件?
答案 0 :(得分:4)
您必须使用CellValueChangedEvent
和CurrentCellDirtyStateChanged
事件。
Private Sub dgv_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellValueChanged
Dim con1 As OleDbConnection = con
con1.Open()
Dim dt As New DataTable
Dim _command As OleDbCommand = New OleDbCommand()
_command.Connection = con1
_command.CommandText = "SELECT * FROM table_name WHERE " & likeContent & ""
dt.Load(_command.ExecuteReader)
Me.dgv.DataSource = dt
con1.Close()
End Sub
Private Sub dgv_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles dgv.CurrentCellDirtyStateChanged
If dgv.IsCurrentCellDirty Then
dgv.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub