C# 如何按用户名对datagrid中的项进行分组
我正在开发wpf应用程序,它用数据库中的项目填充datagrid。 这些物品代表咖啡店的用户和他们赚了多少钱。。。这是一幅这样的图像:C# 如何按用户名对datagrid中的项进行分组,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我正在开发wpf应用程序,它用数据库中的项目填充datagrid。 这些物品代表咖啡店的用户和他们赚了多少钱。。。这是一幅这样的图像: 约翰 现金$192,00 无名氏 现金$75,00 卡丁车75,00 现在我正在填充我的数据网格,如下所示: dtgCashAmount.ItemsSource = null; dtgCashAmount.ItemsSource = BController.GetAllFinancialTransactionsByAllUsers(dateFrom, da
- 约翰
现金$192,00 - 无名氏
现金$75,00
卡丁车75,00
dtgCashAmount.ItemsSource = null;
dtgCashAmount.ItemsSource = BController.GetAllFinancialTransactionsByAllUsers(dateFrom, dateTo);
ListCollectionView collection = new ListCollectionView(DatabaseResults);
collection.GroupDescriptions.Add(new PropertyGroupDescription("Username"));
但我尝试了类似的方法(根据用户名对项目进行分组),但没有成功:
首先,我在我的XAML中添加了Window.Resources
:
<Window.Resources>
<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在我的C#代码中,我尝试过这样的方法:
dtgCashAmount.ItemsSource = null;
dtgCashAmount.ItemsSource = BController.GetAllFinancialTransactionsByAllUsers(dateFrom, dateTo);
ListCollectionView collection = new ListCollectionView(DatabaseResults);
collection.GroupDescriptions.Add(new PropertyGroupDescription("Username"));
基本上我是这样说的:好的,按照用户名对我从数据库中得到的结果进行分组,得到一个结果,正如我在上面的问题(下图)中提供的那样。
但不幸的是,我收到了以下错误:
在使用ItemsSource之前,Items集合必须为空
即使我检查了没有在其他地方设置datagrid源
DataGrid XAML:
<DataGrid Grid.Row="1" IsReadOnly="True" Name="dtgCashAmount " EnableRowVirtualization ="True" EnableColumnVirtualization = "True" GridLinesVisibility="Horizontal" AutoGenerateColumns="False" RowHeaderWidth="0" CanUserAddRows="False">
<GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
<DataGrid.CellStyle>
<StaticResource ResourceKey="DataGridCentering"/>
</DataGrid.CellStyle>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#0091EA"/>
<Setter Property="Opacity" Value="1"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Height" Value="40"/>
</Style>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn x:Name="colPaymentType" Binding="{Binding PaymentType.Title }" Header="Payment type" Foreground="Black" FontSize="{x:Static local:Globals.dataGridfontSizeContent}" FontFamily="Verdana" Width="10*"/>
<DataGridTextColumn x:Name="colKorisnik" Binding="{Binding Username }" Header="Username" Foreground="Black" FontSize="{x:Static local:Globals.dataGridfontSizeContent}" FontFamily="Verdana" Width="10*"/>
<DataGridTextColumn x:Name="colPrice" Binding="{Binding TotalAmount}" Header="Total amount" Foreground="Black" FontSize="{x:Static local:Globals.dataGridfontSizeContent}" FontFamily="Verdana" Width="10*"/>
</DataGrid.Columns>
</DataGrid>
在mm8帮助我之后,现在看起来是这样的:
我在互联网上看到了一个更好的例子(对于用户测试来说,它组织得很好):
mm8 mate有可能实现这样的目标吗 应在
元素中定义GroupStyle
:
<DataGrid.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</DataGrid.GroupStyle>
…而不是作为
元素的直接子元素。发布dtgCashAmount的XAML或显示分组项目的任何元素。是否将GroupStyle放入?是否将一个用户的所有数据显示在一行中,还是相邻显示?将用户“付款”字段更改为带有金额和类型的列表。因此,有一个字段可以显示付款列表shown@mm8这就是他帮助过的伙伴!!谢谢!但是请检查我的编辑,我在底部贴了两张图片。。