【问题标题】:Sort through WPF datagrid using textbox使用文本框对 WPF 数据网格进行排序
【发布时间】:2012-07-02 16:34:21
【问题描述】:

我的应用程序上有一个数据网格,用于从数据库中获取数据。这是通过将其放入数据表然后使用 dataGrid1.ItemsSource = DT.DefaultView 显示它。

我还有一个将用作搜索框的文本框。我希望搜索框搜索数据网格并显示正确的数据。根据用户在搜索框中的输入,不仅显示突出显示,而且实际上使数据消失或重新出现。

我搜索了多个论坛,但我发现的解决方案都不适用于我的应用程序。因此,如果有人能给我一个解决方案,我将不胜感激。

编辑,对问题进行排序

Private Sub txtSearchBox_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles txtSearchBox.TextChanged

    If txtSearchBox.Text = "" Then
        dataGrid1.ItemsSource = DT.DefaultView 'puts the data in to the datagrid
        DT.DefaultView.RowFilter = Nothing
    Else
        chosenFilter = txtSearchBox.Text

        'sets the datagrid filter
        DT.DefaultView.RowFilter = "TYPEID LIKE '%" & chosenFilter & "%'"
    End If

End Sub

【问题讨论】:

    标签: wpf vb.net datagrid


    【解决方案1】:

    有许多选项可以解决您的问题。

    1) 在 CollectionView 上使用过滤器

    在您的代码后面/ViewModel 中创建 ListCollectionView。将数据项放入 collectionsView 并将 DataGrid Itemssoure 绑定到它。然后将过滤器委托传递给 CollectionView,它根据 TextInput 过滤项目。将处理程序附加到文本框 TextChanged 事件,以便在文本更改时刷新/过滤 CollectionView。这里也解释一下:How do I filter items from a collection?

    2) 使用 Jeff Wilcox AutocompleteBox 并实现自定义 SelectionAdapter

    这有点棘手,但恕我直言更优雅。 您需要 Jeff Wilcox AutoCompleteBox,它包含在 WPF Toolkit - 2010 年 2 月版本中。你可以从这里下载它:WPF Toolkit Feb 2010。这里解释了这个非常有用的控件的功能:Using Using the AutoCompleteBox in the WPF Toolkit 和这里AutoCompleteBox control: The missing guide。 这里解释了如何实现自定义选择适配器:Page Up/ Page Down adapter

    【讨论】:

      【解决方案2】:

      将搜索框中的更改应用到网格的 ItemsSource。这就是您所需要的一切。

      【讨论】:

      • 请您进一步解释一下
      【解决方案3】:
      Private Sub txtSearchBox_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles txtSearchBox.TextChanged
      
      If txtSearchBox.Text = "" Then
          dataGrid1.ItemsSource = DT.DefaultView 'puts the data in to the datagrid
          DT.DefaultView.RowFilter = Nothing
      Else
          chosenFilter = txtSearchBox.Text
      
          'sets the datagrid filter
          DT.DefaultView.RowFilter = "TYPEID LIKE '%" & chosenFilter & "%'"
      End If
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2023-02-06
        • 2011-09-22
        • 2014-12-21
        • 1970-01-01
        • 2013-06-02
        • 2013-11-02
        • 2014-05-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多