Search code examples
vb.netdatagridviewtextchanged

How to do textChanged event in datagridview?


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?


Solution

  • 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