I have a DataGrid in WPF where I read the data from an SQL query.I use a DataTable. But now I want all identical lines to be brought together. I thought of the following:
<DataGrid.Columns>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBlock VerticalAlignment="Center" Text="{Binding Name}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Items}" IsReadOnly="True" AutoGenerateColumns="False" HeadersVisibility="None">
<DataGrid.Columns>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
and in CS this:
private void datagrid_Loaded(object sender, RoutedEventArgs e)
{
var groups = (this.Resources["CvsKey"] as CollectionViewSource).View.Groups;
datagrid.ItemsSource = groups;
}
But then he creates 2 empty columns. I do not want the 2 new columns to be created, but that the existing ones are merged. The available columns are generated by my SQL query using a DataTable.
question from:
https://stackoverflow.com/questions/65916749/why-does-the-merging-of-identical-columns-not-work-in-my-datagrid-in-wpf 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…