如何用WPF c#实现数据网格中值和的分组

本文关键字:和的 网格 数据网 WPF 何用 实现 数据 | 更新日期: 2023-09-27 18:06:38

你好,我厌倦了开始编码,这是我的第一个严肃的应用程序。我有分组的数据网格,我想在每组中添加列值的总和,使其看起来像这样https://leeontech.files.wordpress.com/2010/02/final.png

我试着从网上找到许多解决方案,但都不适合我:

我XML

 <DataGrid x:Name="GridRaport" CanUserAddRows="False" VerticalAlignment="Stretch" MinWidth="500" AlternatingRowBackground="LightBlue"  AlternationCount="2" Margin="20,20,20,20">
                    <DataGrid.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.ContainerStyle>
                                <Style TargetType="{x:Type GroupItem}">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                                <StackPanel >
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Margin="10,10,10,10" Text="{Binding Name}" FontWeight="Bold" />
                                                        <TextBlock  Margin="30,10,10,10"  Text="{Binding ItemCount, StringFormat=Liczba wycieczek: {0}}" FontWeight="Bold"  />
                                                    </StackPanel>
                                                    <ItemsPresenter />
                                                </StackPanel>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </GroupStyle.ContainerStyle>
                        </GroupStyle>
                    </DataGrid.GroupStyle>
                </DataGrid>

和我的代码后面

private void FillGridRaport()
    {
        string CmdString = string.Empty;
        using (SqlConnection con = new SqlConnection(ConString))
        {
            CmdString = "Long query";
            SqlCommand cmd = new SqlCommand(CmdString, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable("Wycieczki");
            sda.Fill(dt);
            DataView dataView = dt.AsDataView();
            BindingListCollectionView cv = CollectionViewSource.GetDefaultView(dataView) as BindingListCollectionView;
            PropertyGroupDescription groupDescription1 = new PropertyGroupDescription();
            groupDescription1.PropertyName = "Pracownik";
            cv.GroupDescriptions.Add(groupDescription1);
            GridRaport.ItemsSource = cv;    
}
    }

我将非常感激你的帮助

如何用WPF c#实现数据网格中值和的分组

要得到总和,需要一个转换器。创建一个实现IValueConverter的类,将其作为带键的资源添加,然后在XAML中引用它。

下面是一个示例转换器,我将其设置为将字段名作为ConverterParameter。

public class SumValues : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var cvg = value as CollectionViewGroup;
        var field = parameter as string;
        if (cvg == null || field == null)
            return null;
        // Double field
        return cvg.Items.Sum(r => (double)(r as DataRowView)[field]);
        // Or, if the field is nullable
        //return cvg.Items.Sum(r => (r as DataRowView)[field] as double?); // "as" can return null so we have to use "double?"
        // More complex example - string field that needs to be converted to a long
        //return cvg.Items.Sum(r => long.Parse((r as DataRowView)[field].ToString()));
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

将转换器作为资源添加,可以在App.xaml中添加,也可以本地添加到DataGrid中:

<DataGrid.Resources>
    <local:SumValues x:Key="SumValues" />
</DataGrid.Resources>

最后,将文本块添加到GroupItem的ControlTemplate:

<TextBlock  Margin="60,10,10,10"  Text="{Binding StringFormat=Sum: {0}, Converter={StaticResource SumValues}, ConverterParameter=MyDataSetFieldName}" FontWeight="Bold"  />