在WPF C#中隐藏DataGrid上DataTable中的行 [英] Hide rows from DataTable on DataGrid in WPF C#
问题描述
我试图从DataTable中隐藏特定的行,但是我只能隐藏列.
I'm trying to hide specific rows from DataTable, but I'm only able to hide columns.
我找到了一些与DataGridView相关的解决方案,但这并不相同,因此对我不起作用.
I found some solutions related to DataGridView, but it's not the same and didn't work for me.
在我的xaml文件中,我有:
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;
其中"i"是正确的列号.
where 'i' is proper column number.
如何隐藏行?
推荐答案
我也建议您进行过滤,而且我将不再使用DataTable.您可以使用
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>
在您的ViewModel中
In your ViewModel
myDataTable.Rows[i]["IsVisible"] = false;
如果未设置DataContext,则组件将成为上下文,因此可以在其中添加DataView属性(但可以的话,请使用ViewModels). BoolToVisibilityConverter
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
这篇关于在WPF C#中隐藏DataGrid上DataTable中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!