Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

XML
<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
Comments
[no name] 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"
M.H. Shojaei 7-Jul-12 12:44pm    
I need a code or guidance for filtering datagrid.

XML
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>
 
Share this answer
 
Comments
Mr.VJ 19-Mar-14 0:42am    
great work mohammad hossein .... it helps me a lot ,thank u :)
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!!

C#
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>
 
Share this answer
 
Comments
M.H. Shojaei 7-Jul-12 12:59pm    
I want to filter datagrid column by textBox.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900