vb.net 如何在 datagridview 中执行 textChanged 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21842662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:47:52  来源:igfitidea点击:

How to do textChanged event in datagridview?

vb.netdatagridviewtextchanged

提问by slek

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.

我一直在为我的 datagridview 修复 textChanged 之类的事件,但我无法获得我想要的结果。每当我更改其单元格上的文本时,dataGridView1 必须过滤 dataGridView2 的内容。

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:

这可以过滤我的 dataGridView2 的内容,但在此之前我必须单击 dataGridView1 外的光标/按 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" is where I store the text on my dataGridView1.

“likecontent”是我在 dataGridView1 上存储文本的地方。

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

如何仅通过 dataGridView1 中的 textChanged 事件来更新我的 dataGridView2?

回答by ???ěxě?

You must use the CellValueChangedEventand the CurrentCellDirtyStateChangedevents for this.

为此,您必须使用CellValueChangedEventCurrentCellDirtyStateChanged事件。

 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