This is a migrated thread and some comments may be shown as answers.

Use Aggregrate Functions with AutoGenerateColumns set to True

3 Answers 143 Views
GridView
This is a migrated thread and some comments may be shown as answers.
David Cecil
Top achievements
Rank 1
David Cecil asked on 15 Jan 2010, 01:26 AM
Hello,

Is it possible to use the AggregateFunctions CountFunction with AutoGenerateColumns set to True?  We have a datasource we would like to connect to without defining each columns in xaml or code.  In every example I've always seen the AutoGenerateColumns set to false.  As it stands, if I do the same I will either endup with a "blank" column or duplicate.

Kind regards

3 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 15 Jan 2010, 06:16 AM
Hello David Cecil,

There is no problem to define aggregate functions for autogenerated columns. RadGridView exposes an event called AutoGeneratingColumn which can be used to configure the autogenerated columns. You could try something like:

public Window1()
{
    InitializeComponent();
  
    this.playersGrid.ItemsSource = Club.GetPlayers();
    this.playersGrid.AutoGeneratingColumn += new System.EventHandler<GridViewAutoGeneratingColumnEventArgs>(playersGrid_AutoGeneratingColumn);
}
  
void playersGrid_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
    GridViewDataColumn column = e.Column as GridViewDataColumn;
  
    if (column != null && column.DataMemberBinding.Path.Path == "Number")
    {
        column.AggregateFunctions.Add(new CountFunction() { Caption = "Count" });
    }
}


Best wishes,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Adam
Top achievements
Rank 1
answered on 06 Sep 2013, 09:38 AM


Hello Telerik!,

    I have a similar problem - except that I am using multiple elements within my columns and attempting to show a sum aggregate function(s) on singular properties.
DataTemplate week = new DataTemplate();
 
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(DockPanel));
spFactory.SetValue(DockPanel.LastChildFillProperty, false);
spFactory.SetValue(DockPanel.DataContextProperty, new Binding(e.Column.UniqueName));
 
FrameworkElementFactory quantity = new FrameworkElementFactory(typeof(TextBlock));
quantity.SetBinding(TextBlock.TextProperty, new Binding("Quantity") { StringFormat = "F2" });
quantity.SetValue(TextBlock.WidthProperty, 70D);
quantity.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);
quantity.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right);
quantity.SetValue(TextBlock.PaddingProperty, new Thickness(0, 0, 3, 0));
spFactory.AppendChild(quantity);
 
FrameworkElementFactory value = new FrameworkElementFactory(typeof(TextBlock));
value.SetBinding(TextBlock.TextProperty, new Binding("Cost") { StringFormat = "c" });
value.SetValue(TextBlock.WidthProperty, 80D);
value.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);
value.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Right);
value.SetValue(TextBlock.PaddingProperty, new Thickness(0, 0, 3, 0));
spFactory.AppendChild(value);
 
FrameworkElementFactory rateIndicator = new FrameworkElementFactory(typeof(Image));
rateIndicator.SetBinding(Image.SourceProperty, new Binding("RateIndicator") { Converter = new Convertors.RateIndicatorImageSource() });
rateIndicator.SetBinding(TextBlock.ToolTipProperty, new Binding("RateIndicator") { Converter = new Convertors.RateIndicatorToolTip() });
rateIndicator.SetValue(Image.MarginProperty, new Thickness(3, -1, 0, 0));
rateIndicator.SetValue(Image.HeightProperty, 16D);
rateIndicator.SetValue(Image.WidthProperty, 16D);
spFactory.AppendChild(rateIndicator);
 
week.VisualTree = spFactory;
week.Seal();
 
e.Column.CellTemplate = week;
 
e.Column.IsFilterable = false;
e.Column.IsSortable = false;
e.Column.IsReadOnly = true;
e.Column.FooterTextAlignment = TextAlignment.Right;
                 
int weekNumber = System.Convert.ToInt32(e.Column.UniqueName.Replace("Week", "").Replace("Detail", ""));
 
e.Column.Header = new HoursCostHeader(((PlantReportingViewModel)this.DataContext).StartDate.Value.AddDays((weekNumber - 1) * 7),
(((PlantReportingViewModel)this.DataContext).StartWeekNo.Value + (weekNumber - 1)));
 
((Telerik.Windows.Controls.GridViewDataColumn)e.Column)
    .AggregateFunctions.Add(new Telerik.Windows.Data.SumFunction()
        { SourceField = String.Format("{0}.Cost", e.Column.UniqueName), SourceFieldType = typeof(Decimal) });

 ... However, I am getting the error;
No generic method 'Sum' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.
... which must be because the AggregateFunction binding cannot be located with a straight string assignment (?), but I cannot think how to access the specific dataset which is an ObservableCollection<dynamic>

What am I doing wrong? - with regards to the AggregateFunction, ofcourse!
0
Dimitrina
Telerik team
answered on 11 Sep 2013, 08:31 AM
Hi,

I have tried applying an aggregate function with a SourceField that is a nested property. I did not get it to work and no error appeared.

I can see that you set the Binding for your TextBlock to "Cost". Does it work if you assign the SourceField like so:

...((Telerik.Windows.Controls.GridViewDataColumn)e.Column)
    .AggregateFunctions.Add(new Telerik.Windows.Data.SumFunction()
        { SourceField = "Cost", SourceFieldType = typeof(Decimal) });

Generally, in case you cannot use built-in SumFunction, we suggest using generic aggregate function similar to the "Calculated Column" example on our WPF demos. You can also check the "Custom Aggregate Function" demo.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
David Cecil
Top achievements
Rank 1
Answers by
Milan
Telerik team
Adam
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or