We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to define summary rows using attached property in WPF DataGrid (SfDataGrid)?

Platform: WPF |
Control: SfDataGrid

WPF DataGrid (SfDataGrid) provides support to show the column summary. If the DataContext of SfDataGrid is ViewModel, you can bind SummaryColumns to a property in ViewModel using the AttachedProperty of type List<GridSummaryColumn>.

Refer to the following code example to define the AttachedProperty of type List<GridSummaryColumn>.

public class SfDataGridAttachedProperty
{
      public static readonly DependencyProperty DynamicSummaryColumnsProperty = DependencyProperty.RegisterAttached("DynamicSummaryColumns",
           typeof(List<GridSummaryColumn>),
           typeof(SfDataGridAttachedProperty)
           , new FrameworkPropertyMetadata(null, OnDynamicSummaryColumnsChanged));
 
      public static void SetDynamicSummaryColumns(UIElement element, List<GridSummaryColumn> value)
      {
          element.SetValue(DynamicSummaryColumnsProperty, value);
      }
      public static List<GridSummaryColumn> GetDynamicSummaryColumns(UIElement element)
      {
          return (List<GridSummaryColumn>)element.GetValue(DynamicSummaryColumnsProperty);
      }
 
      private static void OnDynamicSummaryColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
      {
          SfDataGrid grid = d as SfDataGrid;
 
          if (grid.TableSummaryRows.Count() > 1)
          {
              grid.TableSummaryRows.Clear();
          }
 
          GridTableSummaryRow gsr = new GridTableSummaryRow();
          gsr.ShowSummaryInRow = false;
 
          var list = ((List<GridSummaryColumn>)args.NewValue);
 
          foreach (var item in list)
          {
              gsr.SummaryColumns.Add(item);
          }
 
          grid.TableSummaryRows.Add(gsr);
 
    }
}

 

Refer to the following code example to populate GridSummaryColumn in Viewmodel.

internal class ViewModel : INotifyPropertyChanged
{
        private List<GridSummaryColumn> _summarycols;
 
        public List<GridSummaryColumn> SummaryColumns
        {
            get { return _summarycols; }
            set
            {
                _summarycols = value;
                RaisePropertyChanged("SummaryColumns");
            }
        }
 
        public ViewModel()
        {
            PopulateEmployeeDetails();
            SetSummaryColumns();
        }
 
        private void SetSummaryColumns()
        {
            SummaryColumns = new List<GridSummaryColumn>();
            SummaryColumns.Add(new GridSummaryColumn()
            {
                MappingName = "EmployeeID",
                Name = "EmployeeID",
                SummaryType = SummaryType.CountAggregate,
                Format = "Total: {Count}"
            });
            SummaryColumns.Add(new GridSummaryColumn()
            {
                MappingName = "EmployeeSalary",
                Name = "EmployeeSalary",
                SummaryType = SummaryType.DoubleAggregate,
                Format = "Total: {Sum}"
            });
        }
}

 

Refer to the following code example to bind the attached property in SfDataGrid.

<Syncfusion:SfDataGrid x:Name="sfdatagrid"
      local:SfDataGridAttachedProperty.DynamicSummaryColumns="{Binding SummaryColumns}"
      ItemsSource="{Binding EmployeeDetails}">
</Syncfusion:SfDataGrid>

 

View WPF DataGrid Summaries Demo in GitHub.

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile