0

I have a DataGrid and would like to hide certain rows in the DataGrid based on what row index they are. An example of what i mean is that if I have a DataGrid with rows:

Mike  //Index 0
Steve //Index 1
Smith //Index 2
Greg  //Index 3
Jake  //Index 4

and I want to hide all names that start with S, is there a way to do something like

dataGrid1.row[1].Hide; 
dataGrid1.row[2].Hide; 

or something like

dataGrid1.row[1].Visibility = Visibility.Collapsed;
dataGrid1.row[2].Visibility = Visibility.Collapsed;

I do not want to change anything really within my XAML I want to strictly be able to hide a row within my WPF code.

I've seen some really long multiple method ways but is there a way to do this, similarly to

dataGrid1.Columns[1].Visibility = Visibility.Collapsed;

where you can easily hide a column?

Thanks, iato

Edit: What I was looking for was an addition to the potential duplicate question.

            var row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
            row.Visibility = Visibility.Collapsed;

was my final solution :)

iato
  • 356
  • 5
  • 16
  • 1
    `DataGridRow` has a `Visibility` property. In your XAML, use the DataGrid's RowStyle property to set a style on the rows. In the style, write a trigger -- in the XAML -- which, in the XAML, sets the row's Visibility to Collapsed based on some property of the row object. You may want to write a [value converter](https://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter(v=vs.110).aspx) or [multi value converter](https://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter(v=vs.110).aspx). – 15ee8f99-57ff-4f92-890c-b56153 Jun 13 '17 at 16:22
  • If you have a valid reason to avoid XAML, please share. It's rare, but not entirely unheard-of. – 15ee8f99-57ff-4f92-890c-b56153 Jun 13 '17 at 16:25
  • @EdPlunkett Okay thank you I will try this. I typically like to do my Screen Design in XAML, while having my Screen Population, Interaction, Logic and Business Rules in WPF. – iato Jun 13 '17 at 16:31
  • @EdPlunkett I am also fairly new to XAML but have used c# for some time so I am more comfortable with WPF to XAML – iato Jun 13 '17 at 16:32
  • Possible duplicate of [Get DataGrid row by index](https://stackoverflow.com/questions/21413259/get-datagrid-row-by-index) – ASh Jun 13 '17 at 16:37
  • 1
    @ASh I just want to be able to hide the rows, I already know what Index my rows are on. – iato Jun 13 '17 at 16:51
  • @iato If you how how the datagrid is populated, I can write an example showing how to do this. – 15ee8f99-57ff-4f92-890c-b56153 Jun 13 '17 at 16:56
  • 2
    @iato, read again. the post shows how to get DataGridRow object by known index. Then you can do `row.Visibility = Visibility.Collapsed;` – ASh Jun 13 '17 at 16:59
  • @ASh would you look at that! That worked perfectly and was what I was wanting!! Thank you so much !! – iato Jun 13 '17 at 17:15

1 Answers1

0

Data bind the grid to a collection in the ViewModel. In the Data Template use a binding for the visibility of outermost element and a converter to convert the name field in the collection to visibility.

<DataTemplate>
    <Grid Visibility={Binding Name, Converter={{StaticResource NameToVisibilityConverter}} >
    ...
    </Grid>
</DataTemplate>

In the Converter - for example:

Convert() { return name[0] == 'S' ? Visibility.Hidden : Visibility.Visible; }