2012-07-02 91 views
0

我的应用程序上有一个datagrid从数据库中获取数据。这是通过将其放入数据表然后使用dataGrid1.ItemsSource = DT.DefaultView来显示它。使用文本框对WPF数据网格进行排序

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

我有通过多个论坛搜索,但没有找到我的应用程序的解决方案。所以如果有人能给我一个解决方案,我会非常感激。

编辑,排序问题

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 

回答

0
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 
0

在搜索框将更改应用于电网的ItemsSource。这就是你需要的一切。

+1

你能解释进一步请 – TeamGB

2

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

1)上的CollectionView

使用过滤器在你的代码中创建的ListCollectionView背后/视图模型。将数据项放入collectionsView并将DataGrid ItemSoure绑定到它。然后将一个Filter委托传递给CollectionView,根据TextInput过滤项目。将处理程序附加到Textbox TextChanged事件,以便在文本发生更改时刷新/过滤CollectionView。这也说明如下:How do I filter items from a collection?

2)使用杰夫·威尔考克斯AutocompleteBox和实施定制SelectionAdapter

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

+0

使用过滤器是我做的,代码如下 – TeamGB

相关问题