Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML WPF DataGrid
Hi,
This is a Datagrid that i need to search or filter name or family of people by text box. how can i write code for TextChanged event. what's its method simply?
 
 <Grid>
        <DataGrid Margin="3,33,145,43" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" HeadersVisibility="All" HorizontalGridLinesBrush="DarkGray"
                  Name="grdPersonnel1" AlternatingRowremoved="LightGray" VerticalGridLinesBrush="DarkGray" HorizontalScrollBarVisibility="Auto" FontFamily="Courier New" FontSize="12" 
                  FontWeight="Black" IsTextSearchEnabled="True" BorderBrush="DarkGray" VerticalContentAlignment="Center" IsReadOnly="True" >                             
            
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=ID}" Header="ID" Width="Auto"></DataGridTextColumn>                
                <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" Width="Auto" ></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Path=Family}" Header="Family" Width="Auto"></DataGridTextColumn> 
            </DataGrid.Columns> 
        </DataGrid>
    </Grid>
	
	 <TextBox Grid.Row="7" Height="23" HorizontalAlignment="Left" Margin="75,13,0,0" Name="txtFilter" VerticalAlignment="Top" Width="120" TextChanged="txtFilter_TextChanged" />
Posted 6-Jul-12 7:22am
Comments
Wes Aday at 6-Jul-12 14:57pm
   
Perhaps you should explain what you mean by "how can i write code for TextChanged event. what's its method simply"
Mohammad Hossein Shojaei at 7-Jul-12 12:44pm
   
I need a code or guidance for filtering datagrid.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should look at the ICollectionView.
 
This is great for filtering data used in WPF!
 
However, a little more detail would have been nice. i.e what are you trying to do? what have you tried etc, are you using MVVM / Code behind!!
 
ICollectionView view = CollectionViewSource.GetDefaultView(newValue);
IEqualityComparer<string> comparer = StringComparer.InvariantCultureIgnoreCase;
view.Filter = o => { 
                     Person p = o as Person; 
                     return p.FirstName.Contains(SearchedText, comparer) 
                            || p.LastName.Contains(SearchedText, comparer); 
                   }</string>
  Permalink  
Comments
Mohammad Hossein Shojaei at 7-Jul-12 12:59pm
   
I want to filter datagrid column by textBox.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I resolved it:
 
<pre lang="c#"> private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Personel.mdf;Integrated Security=True;User Instance=True");
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = new SqlCommand();
                adapter.SelectCommand.Connection = con;
                adapter.SelectCommand.CommandText = "SELECT * FROM Personnels where Name like  N'%" + textBox1.Text + "%'";
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                grdPersonnel1.DataContext = dt;
            }
            catch (Exception) { }
        }</pre>
  Permalink  
Comments
Mr.VJ at 19-Mar-14 0:42am
   
great work mohammad hossein .... it helps me a lot ,thank u :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 8 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100