【问题标题】:Filter data in DataGrid在 DataGrid 中过滤数据
【发布时间】:2017-07-27 17:32:05
【问题描述】:

我将如何过滤我的数据网格之一中的数据,例如:

我有一个mysql类:

Public Function getArtikli() As DataTable
    Dim query As String = "select id,named,group,art,vpc,mpc,currency,foot,pc from demo.artikli"
    Dim table As New DataTable()
    Using connection As New MySqlConnection(konekcija)
        Using adapter As New MySqlDataAdapter(query, connection)

            adapter.Fill(table)
            Return table
        End Using
    End Using
End Function

这是来自 mainwindow.vb 的函数

Private Function fillGrids()
    gornjiGrid.ItemsSource = mysql.getArtikli().DefaultView
    gornjiGrid.Items.Refresh()
End Function

我想使用 textchanged 事件过滤名为“gornjiGrid”的网格中返回的行,用于以下文本框:

Private Sub textBox1_Copy1_TextChanged(sender As Object, e As TextChangedEventArgs) Handles textBox1_Copy1.TextChanged
    'mysql.getArtikli().DefaultView.RowFilter = " name like '*" & textBox1_Copy1.Text & "*'"
End Sub

最后这是我的数据网格 xml:

<DataGrid ItemsSource="{Binding gornjiGrid}" x:Name="gornjiGrid"  Margin="376,141,15,0" FontSize="12" Height="126" VerticalAlignment="Top"/>

【问题讨论】:

    标签: mysql wpf vb.net datagrid


    【解决方案1】:

    您应该过滤内存中的DataView。还要确保正确拼写列名。您的查询似乎没有返回“name”列,但有一个“named”列。

    试试这个:

    Private Sub textBox1_Copy1_TextChanged(sender As Object, e As TextChangedEventArgs) Handles textBox1_Copy1.TextChanged
        Dim dataView = CType(gornjiGrid.ItemsSource, System.Data.DataView)
        If String.IsNullOrEmpty(textBox1_Copy1.Text) Then
            dataView.RowFilter = Nothing
        Else
            dataView.RowFilter = " named like '*" & textBox1_Copy1.Text & "*'"
        End If
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      我强烈建议您将 MVVM 模式用于 WPF 应用程序。 将ListCollectionView 用于DataGrid.ItemsSource,将ObservableCollection(Of ...) 用于ListCollectionView 的项目。然后使用ListCollectionView.Filter 谓词过滤DataGrid 中的项目。您会对当前的老式 Windows 窗体方法感到非常失望和愤怒。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-17
        • 2013-03-12
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        • 2011-02-12
        • 2016-06-23
        • 2011-05-09
        相关资源
        最近更新 更多