lordhusnain
lordhusnain

Reputation: 97

Find a record in Wpf datagrid by typing

I have a datagrid which is bound to Observable Members collection. Now i want to find a member in datagrid by typing the member name. I have tried IsTextSearchEnable property but it is not searching. Here is my xaml.

<DataGrid Name="dgOtherCharges" AutoGenerateColumns="False" RowHeight="25" Grid.Row="4" AlternatingRowBackground="{StaticResource AlternateRowBackgroundBrush}" Grid.ColumnSpan="3" IsTextSearchEnabled="True" TextSearch.Text="Name"
              CanUserAddRows="False" HeadersVisibility="Column" SelectionUnit="Cell" ItemsSource="{Binding Path=MembersCollection,Mode=TwoWay}"  HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
              VerticalGridLinesBrush="{StaticResource GridLineColorBrush}" HorizontalGridLinesBrush="{StaticResource GridLineColorBrush}">
            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <Trigger Property="DataGridCell.IsSelected" Value="True">
                            <Setter Property="Background" Value="{StaticResource ButtonSelectedBrush}"  />
                            <Setter Property="BorderBrush" Value="#A8E3FC" />
                            <Setter Property="Foreground" Value="Black" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>

            <DataGrid.Columns>

                <DataGridCheckBoxColumn Binding="{Binding IsCheck,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="50">
                    <DataGridCheckBoxColumn.ElementStyle>
                        <Style TargetType="CheckBox">
                            <Setter Property="HorizontalAlignment" Value="Center"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </DataGridCheckBoxColumn.ElementStyle>
                </DataGridCheckBoxColumn>
                <DataGridTextColumn Header="Member Name" Binding="{Binding Name,Mode=TwoWay}" IsReadOnly="True" Width="*"/>


            </DataGrid.Columns>
        </DataGrid>

i need a quick solution folks

Upvotes: 3

Views: 8666

Answers (2)

Herman Cordes
Herman Cordes

Reputation: 4976

I stumbled upon this question today to find a solution to the same problem. While TextSearch apparently was not available back in 2012, it currently is.

So for future reference, below my current solution for this problem with three added properties: IsTextSearchCaseSensitive, IsTextSearchEnabled and TextSearch.TextPath.

<DataGrid ItemsSource="{Binding Path=Directories, Mode=OneWay}"
          SelectedItem="{Binding Path=SelectedDirectory, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          EnableRowVirtualization="False"
          EnableColumnVirtualization="True"
          VirtualizingStackPanel.IsVirtualizing="False"
          VirtualizingStackPanel.VirtualizationMode="Standard"
          IsTextSearchCaseSensitive="False"
          IsTextSearchEnabled="True"
          TextSearch.TextPath="Name">
   <DataGrid.Columns>
      <DataGridTemplateColumn Header="Name" SortMemberPath="Name" />
   </DataGrid.Columns>
</DataGrid>

Upvotes: 9

Dtex
Dtex

Reputation: 2623

<TextBox TextChanged="TextBox_TextChanged"/>

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
      var view = CollectionViewSource.GetDefaultView((DataContext as MyViewModel).MembersCollection);
      view.Filter = o => (o as Member).Name.Contains((sender as TextBox).Text);
}

I hope it was quick enough :)

Upvotes: 5

Related Questions