Resolved Filter Listbox using textbox

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
52
Programming Experience
Beginner
I have done this in a Winform application, and understand how that is done, but in WPF, I don't have a binding source instead I have a DataTable/DataView with a {Binding Column} in the XAML file.

So when I come to filter the Listbox I am struggling a little - either getting a syntax error or simply nothing is happening when I type.

Code for populating:

C#:
System.Data.DataTable table = new System.Data.DataTable();

C#:
DataColumn codeColumn = new DataColumn("Code", typeof(string));
            DataColumn projColumn = new DataColumn("Project", typeof(string));

            dr = cmd.ExecuteReader();

            table.Columns.Add(codeColumn);
            table.Columns.Add(projColumn);

            table.Load(dr);

            DataView view = new DataView(table);
            codeList.ItemsSource = view;
            projList.ItemsSource = view;

This works fine, and with the XAML below, I have two list boxes, linked as I wanted them to be, and when I double click on an entry in one list box the other textbox populates with the selected item from the other list...which is perfect.

XAML:

C#:
<ListBox x:Name="codeList" Grid.ColumnSpan="2" Grid.RowSpan="3" Grid.Column="1" Grid.Row="7" FontSize="10" ItemsSource="{Binding codeColumn}" DisplayMemberPath="Code" SelectedValuePath="Code" IsSynchronizedWithCurrentItem="True" MouseDoubleClick="codeList_MouseDoubleClick"/>

The projList is exactly the same.

I was attempting to filter with:

C#:
DataView code = new DataView(table);

            code.RowFilter = $"Code LIKE '*{codEntryTB.Text}'";

I have tried a lot of variations of this, based on the MSDN site (though I find that quite lacking on this particular issue - having a single example of "Code < 3", and various other tutorial sites - I can't see why it is not working.

I feel like I am missing something, like refreshing the codeList after the filter so that the filter takes effect, but every tutorial I have seen omits this as if it is not necessary and should happen automatically, so a little lost on why nothing is happening.
 
Solution
From what I can tell, you have told codeList and projList to bind to the to the DataView instance in the variable view. Now you are creating another DataView instance and applying a filter to it. How do you expect, codeList and projList to know about this new instance?

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,416
Location
Chesapeake, VA
Programming Experience
10+
From what I can tell, you have told codeList and projList to bind to the to the DataView instance in the variable view. Now you are creating another DataView instance and applying a filter to it. How do you expect, codeList and projList to know about this new instance?
 
Solution

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
52
Programming Experience
Beginner
That is why a second pair of eyes is always useful.

Yes, of course they are not even slightly linked, how could it possibly know.....I moved it to a public variables for use through-out and of course it works perfectly.

Thanks for taking a minute to look and let me know Skydiver
 
Top Bottom