开发者

Problem with filtering DataGrid

开发者 https://www.devze.com 2023-01-16 19:11 出处:网络
I have bound my DataGrid to a DataTableand only few of the details are displayed in the grid. When I wanted to filter the DataGrid I created a View with my DataGrid\'s ItemsSource.

I have bound my DataGrid to a DataTable and only few of the details are displayed in the grid. When I wanted to filter the DataGrid I created a View with my DataGrid's ItemsSource. Code:

  Dim myView As ICollectionView = CollectionViewSource.GetDefaultView(MyDGrid.ItemsSource)
  myView.Filter = New Predicate(Of Object)(AddressOf filterFunc1)

Now When I do the search, the non-displayed fields are also included in the search.

Public Function filterFunc1(ByVal obj As Object) As Boolean
    Dim filStr As String = "*" & TextBox1.Text & "*"
    For Each item As String In obj.Row.ItemArray
        **If item.ToLower Like filStr.ToLower Then**
            Return True
        End If
    Next
    Return False
End Function

Also I Have ComboBox fields in the DataGrid which are loaded separately from other DataTable's. Now I cant Include them in the search.

A screenshot from my App:

Problem with filtering DataGrid

So how do I make a search that includes only the Text from Displ开发者_开发问答ayed part.

EDIT: Also how do I skip searching the null valued fileds? 'cause thats causing an exception in my case.


Well then... Your question is pretty disjointed and I can't understand all of it - maybe that's why you didn't get an answer so far. Skipping null fields is simply a matter of adding a new condition in filterFunc1 - if Convert.IsDBNull(item) then continue for (assuming item is a field in a DataRow, of course).

However, this programming style is pretty foggy and I'd recommend at the very least being more clear on which columns you filter, and the types of objects in the columns. A much better approach would be to map the data you're getting from the database to actual objects in your application - that allows for more type-safe programming. I think the main problem here is that nobody can really tell what's going on there from a few lines of code because nobody can make any assumptions about what kind of objects are there.

About the items in the ComboBox, no idea what kind of difficulties you're having, you might want to clear that up a bit. you could maintain, instead of simply strings, structures containing both captions and IDs, like say

public class YourComboItem
  public property Id as string [get/set]
  public property Title as string [get/set]
end class

Then bind your ComboBox's ItemsSource to a collection of these items retrieved from the database, and set DisplayMemberPath to Title and ValueMemberPath to Id. Then you can use the ComboBox's SelectedValue to get the selected ID. As you can see, having objects instead of raw data structures can have quite some advantages.

Of course, I described getting the SelectedValue directly from the ComboBox, while a much better architecture would be MVVM, with the ViewModel containing an ObservableCollection(Of YourComboItem) and the ComboBox's ItemSource bound to it with an actual binding. Then you can also bind the SelectedItem to a property in your ViewModel, and have the item as a whole, including both Id and Title, to work with without knowing anything about your user interface. Alternatively you could have an ICollectionView generated from the collection of items and bind the ItemsSource to that, then you'd have the selected item in the ICollectionView's CurrentItem property.

I'd really recommend reading up on MVVM (Model-View-ViewModel) to make your work with WPF a whole lot easier.

0

精彩评论

暂无评论...
验证码 换一张
取 消