2016-09-12 33 views
1

你好我很疲倦开始编码,这是我第一次认真的应用程序。我有一个分组数据网格,我想从列值的各组和添加,使其看起来像这样 https://leeontech.files.wordpress.com/2010/02/final.png如何在数据网格中添加数值总和与分组WPF c#

我尝试使用从互联网上,但没有工作的许多解决方案,对我来说:

我的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;  
} 
    } 

代码我会厌倦感谢您的帮助

回答

2

为了得到总和,你需要一个转换器。创建一个实现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的控件模板:

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