如何在datagridview中执行textChanged事件? [英] How to do textChanged event in datagridview?
问题描述
我一直在为datagridview修改textChanged like事件,但是我无法获得我想要的结果。当我更改其单元格上的文本时,dataGridView1必须对dataGridView2的内容进行过滤。
I've been fixing the textChanged like event for my datagridview but I was not able to get the result that I wanted. The dataGridView1 must filter the content of dataGridView2 whenever I changed a text on its cell/s.
这可以过滤我的dataGridView2的内容,但在此之前,我必须单击dataGridView1之外的光标/按Tab。这是我的代码:
This can filter the content of my dataGridView2 but before that I must click the cursor outside the dataGridView1/press Tab. Here is my code:
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上的文本。
"likecontent" is where I store the text on my dataGridView1.
我的dataGridView2将如何通过textChanged like event从我的dataGridView1更新?
How will my dataGridView2 be updated just by textChanged like event from my dataGridView1?
推荐答案
您必须使用 CellValueChangedEvent
和 CurrentCellDirtyStateChanged
事件。 / p>
You must use the CellValueChangedEvent
and the CurrentCellDirtyStateChanged
events for this.
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
这篇关于如何在datagridview中执行textChanged事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!