【问题标题】:how to add sum of values in datagrid with grouping WPF c#如何通过分组WPF c#在datagrid中添加值的总和
【发布时间】:2017-01-20 07:38:07
【问题描述】:

您好,我开始编码时很累,这是我的第一个认真的应用程序。我有分组的数据网格,我想在每组中添加列中的值总和,使其看起来像这样 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;    
}
    }

我会很感激你的帮助

【问题讨论】:

    标签: c# wpf datagrid sum grouping


    【解决方案1】:

    要得到总和,您需要一个转换器。创建一个实现 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"  />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 2018-03-25
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多