如何在vb.NET中的dataGridView中添加搜索功能

时间:2011-09-25 15:15:43

标签: vb.net winforms vb6-migration

如何从datagridView中的选定行中选择单个单元格,并在选择我想要放置简单搜索功能后(就像我们在Windows文件夹中一样 - 键入任何字符并搜索应该有效)?

2 个答案:

答案 0 :(得分:0)

我真的不明白你的问题。如果要选择一个单元格,可以使用celldoubleclick事件作为示例。并且要获取所选单元格,请使用e.rowindex和e.columnindex,它将为您提供单元格所在的行和列。

答案 1 :(得分:0)

您可以尝试将其作为可能的解决方案。

Dim nwData as CustomersDataSet = CustomersDataSet.GetCustomers()
m_CustomersGrid.DataSource = m_CustomersBindingSource
m_CustomersBindingSource.DataSource = nwData.Customers

Then you can sort using the BindingSource.

CustomersBindingSource.Sort = "ContactName ASC"

And you can find using the BindingSource.

Dim index as integer = _
CustomersBindingSource.Find("CompanyName", CompanyNameTextBox.Text)
If index <-1 then 'it was found; move to that position
CustomersBindingSource.Position = index
End If

然后你可以填充:

CustomersBindingSource.Find("CompanyName", CompanyNameTextBox.Text)

通过利用以下方式捕获单元格中的键:

 Private Sub DataGridView1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyUp
        Dim dgv As DataGridView = TryCast(sender, DataGridView)

        If dgv IsNot Nothing Then

           'You will need some logic here to determine how long to wait between keyups 
           'Perhaps a timer that ticks every500 milliseconds and reset on keyup.  
e.KeyData
            End If
        End Sub

我在This Location

找到了原始的Biding Source Logic