【问题标题】:How to do textChanged event in datagridview?如何在 datagridview 中执行 textChanged 事件?
【发布时间】:2014-02-18 01:29:07
【问题描述】:

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

这可以过滤我的 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”是我在 dataGridView1 上存储文本的位置。

我的 dataGridView2 将如何仅由我的 dataGridView1 中的 textChanged 类事件更新?

【问题讨论】:

标签: vb.net datagridview textchanged


【解决方案1】:

您必须为此使用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

【讨论】:

  • 感谢@Mr CodeXer。现在它起作用了!我将“dgv”替换为“dataGridView1”('Me.dgv.DataSource = dt'除外),因为它是事件发生的地方。
猜你喜欢
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
相关资源
最近更新 更多