How to do textChanged event in datagridview?

我怕爱的太早我们不能终老 提交于 2019-12-23 05:01:46

问题


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.

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" is where I store the text on my dataGridView1.

How will my dataGridView2 be updated just by textChanged like event from my dataGridView1?


回答1:


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


来源:https://stackoverflow.com/questions/21842662/how-to-do-textchanged-event-in-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!