Hi
I have a winforms app I'm trying to port to WPF. In that I have a datagridview bound to a bindingsource and use
BindingSource.Filter = string.Format("Column LIKE '%{0}%'", tb_Filter.Text);
in a textchanged event method to let the user filter the datagrid in real time.
I'd like to do something similar in my wpf application.
When I bind the datagrid directly to a dataset, I can use the RowFilter statement on the table's DefaultView and it works, but it's really laggy with over 500 rows in the datagrid.
So I thought I'd try to bind it to and ICollectionView or ListCollectionView instead.
I'm gonna post most of the XAML for the window and relevant code.
XAML looks like this
<Window x:Class="Test_DataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:Test_DataGrid"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="531" Width="769">
<Window.Resources>
<ObjectDataProvider x:Key="ConDataProvider"
ObjectType="{x:Type local:ConDataProvider}"/>
<ObjectDataProvider x:Key="con_FullGrid"
ObjectInstance="{StaticResource ConDataProvider}"
MethodName="GetIcol"/>
</Window.Resources>
<Grid Name="mainGrid" DataContext="{Binding Source={StaticResource con_FullGrid}}">
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Height="423" HorizontalAlignment="Left" Margin="36,49,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="687" FrozenColumnCount="20" RowHeight="20">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=cID}" CanUserReorder="False" CanUserResize="False" CanUserSort="False" Header="" />
<DataGridTextColumn Binding="{Binding Path=Nafn}" CanUserReorder="False" CanUserResize="False" CanUserSort="False" Header="" />
//some more column definitions here
</DataGrid.Columns>
</DataGrid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="147,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" TextChanged="textBox1_TextChanged" />
</Grid>
</Window>
Here is the ConDataProvider class that the DataGrid is bound to. Gets the data into a dataset from a mysqldataadapter and puts it into either a ListCollectionView or an ICollectionView. Not sure which one I should use. Maybe neither?
class ConDataProvider
{
private MySqlDataAdapter adapter;
private Data data;
private ListCollectionView lcolview;
private DataSet dataset;
private ICollectionView ICol;
public ConDataProvider()
{
dataset = new DataSet();
data = new Data();
adapter = data.getAdapter("SELECT * FROM con_FullGrid");
adapter.Fill(dataset, "con_FullGrid");
}
public ListCollectionView GetView()
{
lcolview = new ListCollectionView(dataset.Tables["con_FullGrid"].DefaultView);
return lcolview;
}
public ICollectionView GetIcol()
{
ICol = CollectionViewSource.GetDefaultView(dataset.Tables["con_FullGrid"].DefaultView);
return ICol;
}
}
Now I'm not sure where I'd go from here.
Can I apply a filter to the same instance of the ConDataProvider that is used in XAML and how would I filter the collection in a similar way as I did with the bindingsource?
Also I've been wondering how I can keep the datagrid in sync with the mysql database. It's gonna be read only, but I'd like it to update whenever data is updated in the database.
I've been trying to figure this out for hours, but all I've found about this is really confusing to me at the moment :)
Hoping someone could point me in the right direction.
Thanks in advance.