数据网格视图+搜索文本框中的自动筛选



我有一个带有地址表的访问数据库,其中包含不同类型的客户端。带有";类型";以及包含来自该客户端的数据的列(地址、名称等(。

我有3个按钮,可以打开主屏幕上的单独面板,显示2个最相关的地址,1个显示所有地址。在那个屏幕上,我想通过使用当他们按下搜索按钮时会出现的文本框进行过滤。

我正试图将数据网格视图拖到屏幕上,现在我需要知道我应该向文本框添加什么代码,使其过滤数据网格视图中的地址。我当前该页面的代码在下面

Public Class Patienten

Private Sub btnAddPatient_Click(sender As Object, e As EventArgs) Handles btnAddPatient.Click
'maak hier een nieuwe klantenfiche in een nieuwe form (mainpatientform)
'Dim Pat_frm As New MainPatientform
'Pat_frm.display(MainPatientform.soortenum.nieuw)
NewRelationForm.Show()
End Sub
Private Sub Tbl_RelatiesBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.Tbl_RelatiesBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.PatientenDatabaseDataSetX)
End Sub
Private Sub Patienten_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'PatientenDatabaseDataSetX.tbl_Relaties' table. You can move, or remove it, as needed.
Me.Tbl_RelatiesTableAdapter.Fill(Me.PatientenDatabaseDataSetX.tbl_Relaties)
End Sub
Private Sub Tbl_RelatiesDataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles Tbl_RelatiesDataGridView.CellContentClick
MainPatientform.Show()
MainPatientform.TabControl1.SelectedIndex = 0

End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
gbxSearhbox.Visible = True
End Sub
Private Sub txtSearchPatient_TextChanged(sender As Object, e As EventArgs) Handles txtSearchPatient.TextChanged
'no idea what to put in here
End Sub
End Class

我的另外两个屏幕还没有代码(首先需要这个(,但这里我需要一个";"自动滤波器";对于一种类型的客户端。所以当我打开这个列表时,我只得到类型为"的客户端;x〃;但由于我甚至无法在主屏幕中进行过滤,如何也进行过滤。。。我是的初学者

您有两个选项,

  1. 您在ms访问中过滤数据,然后将其放入数据表
  2. 您可以通过以下操作以编程方式筛选数据:

这是一个c#示例,在vb中有一个等效的代码。

var data = from x in myDataTable.asEnumerable()
where x.Field<string>("type") == "typeofClient"
select x;

通过编程过滤后,可以将其附加到datatable/dataadapter,然后将其显示在datagridview中。

请参阅本文档。DataTable.DefaultView属性

样本代码:

Dim connection As New SqlConnection(" server info")
Dim sda As New SqlDataAdapter("select * from table", connection)
Dim dt As New DataTable
sda.Fill(dt)
dt.DefaultView.RowFilter = "type = 'x'"
DataGridView1.DataSource = dt

感谢您的想法。

最简单的答案是在我填充网格后简单地添加一行:

Private Sub Patienten_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'PatientenDatabaseDataSetX.tbl_Relaties' table. You can move, or remove it, as needed.
Me.Tbl_RelatiesTableAdapter.Fill(Me.PatientenDatabaseDataSetX.tbl_Relaties)
Me.Tbl_RelatiesBindingSource.Filter = "Rel_Type='Patient'" 'This line :-)

筛选器绑定源是关键!非常感谢@jmcilinney的评论!

最新更新