1

I'm trying to hide specific rows from DataTable, but I'm only able to hide columns.

I found some solutions related to DataGridView, but it's not the same and didn't work for me.

In my xaml file I have :

<DataGrid Visibility="Visible" HorizontalAlignment="Stretch" Name="dataGrid_first" VerticalAlignment="Stretch" Width="Auto"  Grid.Column="1" Grid.Row="2" >

</DataGrid>

To show data on it, I'm doing as follows:

dataGrid_first.ItemsSource = myDataTable.AsDataView();

And it's working. To hide columns, I'm doing something like this:

dataGrid_first.Columns[i].Visibility = Visibility.Hidden;

where 'i' is proper column number.

How can I hide a row?

Tatarinho
  • 754
  • 2
  • 11
  • 31
  • what are conditions to hide specific rows? one simple approach is to use DataView `Filter` property – ASh Nov 22 '16 at 08:29
  • I have two similar datagrid and I'm trying to show differences between two databases. Are you able to wrote some simple example? – Tatarinho Nov 22 '16 at 08:33
  • Why give the grid the data and then try to hide some of it. Why not just give it the data you want it to show. In other words filter the data first. – Trevor Cousins Nov 22 '16 at 08:43
  • It's ok for me to give proper data. The problem is that I'm not able to filter proper rows. – Tatarinho Nov 22 '16 at 08:46

1 Answers1

0

I would recommend filtering as well, and more I would not use DataTable a bit old. You can hide row with

<DataGrid ItemsSource="{Binding DataView}">
                <DataGrid.RowStyle>
                    <Style TargetType="DataGridRow">
                        <!-- your column name, visibility was not working if there is no converter -->
                        <Setter Property="Visibility" Value="{ Binding Path=IsVisible ,Converter={StaticResource BoolToVisibilityConverter}}"></Setter>
                    </Style>
                </DataGrid.RowStyle>
            </DataGrid>

In your ViewModel

myDataTable.Rows[i]["IsVisible"] = false;

If you don't set a DataContext your component will be the context so you can add DataView property there (But use ViewModels if you can). BoolToVisibilityConverter

Filip Cordas
  • 2,531
  • 1
  • 12
  • 23