views:

38

answers:

1

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: A screenshot from my App:

So how do I make a search that includes only the Text from Displayed part.

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

A: 

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.

Alex Paven
Hi Alex, Thanks for the reply. Firstly, the error with DBNull is in the `For Each`. That it cant convert a DBNull into String. Exactly where do I put the convertion syntax? Second, I have not imposed a column wise search but an over all grid wise search. As you say that might be a bad practise. I ll change it!. And why do you say that I should retrieve only fields that I am gonna use? And with the combobox thing, My main table with which I have created the View for filter doesnt have the values directly instead their id's. say status ID. Now I cant search the combobox's with the View I have.
Sarath
Using Linq, you could say `for each item in obj.Row.ItemArray.OfType(String)()` (that's off the top of my head so not sure the syntax is correct, haven't worked in VB in ages). Also, the full-grid search should have been obvious, yes; in this case your approach isn't all that bad, what I was complaining about was searching directly against a DataTable... which limits extensibility etc. As for the ComboBox, you have little choice aside from keeping both the captions and IDs of the elements loaded and getting the IDs when doing the search; I'll update my answer with a suggestion.
Alex Paven
The technique you have suggested is more or less the same as mine. Instead of having a class I am using a DataTable that has the Id and Status name fields. My DataGridComboBox is bound the this source with the statusId(from main table) linked. But my problem is that, my filter function performs the search on **main table** (View) which doesn't have the actual values(say Medium,Low) but id's(3,6). If U want I shall post my table structures here. And I am new to .Net Alex. I haven't started reading MVVM. Sure will :)
Sarath