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

Apply aggregate function on groupby

0 Answers 85 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sufyan
Top achievements
Rank 1
Sufyan asked on 15 Nov 2012, 03:44 PM
<Window x:Class="AggregateFunctions.MainWindow"
         WindowState="Maximized"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <telerik:RadGridView HorizontalAlignment="Stretch" Margin="5" Name="radGridView1" VerticalAlignment="Stretch" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3"
                             AutoGenerateColumns="True" CanUserDeleteRows="False" CanUserInsertRows="False" CanUserSelect="False" GridLinesVisibility="Horizontal"
                             ShowInsertRow="False" ShowGroupFooters="False" ShowColumnHeaders="True" RowIndicatorVisibility="Collapsed" FrozenColumnCount="3"
                             AlternationCount="2"  Loaded="radGridView1_Loaded"
                             AlternateRowBackground="#F8F8F8" AutoGeneratingColumn="radGridView1_AutoGeneratingColumn">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Company Name" DataMemberBinding="{Binding CompanyName}" Width="180" IsReadOnly="True"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Ticker" DataMemberBinding="{Binding Ticker}" Width="80" IsReadOnly="True"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="MIC" DataMemberBinding="{Binding MicCode}" IsReadOnly="True"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Publisher" DataMemberBinding="{Binding Publisher}" IsReadOnly="True"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Industry" DataMemberBinding="{Binding Industry}" IsReadOnly="True"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Model Name" DataMemberBinding="{Binding RegressionName}" Width="180" IsReadOnly="True"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Multiple" DataMemberBinding="{Binding Multiple}" Width="120" IsReadOnly="True"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Qtrs Lead" DataMemberBinding="{Binding NumQtrsLead}" IsReadOnly="True"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Time Period" DataMemberBinding="{Binding TimePeriod}" DataFormatString="{}{0:MM/dd/yyyy}"  IsReadOnly="True"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Model Case" DataMemberBinding="{Binding ModelCase}" IsReadOnly="True"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Is Default" DataMemberBinding="{Binding IsDefault}" IsReadOnly="True"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="R. Squared" DataMemberBinding="{Binding RSquared}" IsReadOnly="True" TextAlignment="Right"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Indicator Percentile" DataMemberBinding="{Binding IndicatorPercentile}" DataFormatString="{}{0:P2}" TextAlignment="Right"  IsReadOnly="True"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Over Under Valuation" DataMemberBinding="{Binding OverUnderValuation}" DataFormatString="{}{0:P2}" TextAlignment="Right" IsReadOnly="True"></telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using Telerik.Windows.Controls;
 
namespace AggregateFunctions
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<string> lstColumns = new List<string>();       
        public MainWindow()
        {
            InitializeComponent();
 
            lstColumns.Add("CompanyName");
            lstColumns.Add("Ticker");
            lstColumns.Add("MicCode");
            lstColumns.Add("RegressionName");
            lstColumns.Add("Publisher");
            lstColumns.Add("Industry");
            lstColumns.Add("Multiple");
            lstColumns.Add("NumQtrsLead");
            lstColumns.Add("TimePeriod");
            lstColumns.Add("ModelCase");           
            lstColumns.Add("RSquared");
            lstColumns.Add("IndicatorPercentile");
            lstColumns.Add("OverUnderValuation");
        }
 
        private void radGridView1_AutoGeneratingColumn(object sender, Telerik.Windows.Controls.GridViewAutoGeneratingColumnEventArgs e)
        {
            if (lstColumns.Contains(e.Column.UniqueName))
                e.Cancel = true;
            else
            {
                e.Column.TextAlignment = TextAlignment.Right;
                e.Column.Width = new GridViewLength(110);
                e.Column.IsReadOnly = true;
            }
        }
 
        private DataTable LoadData()
        {
            int counter = 0;
            DataTable dt = new DataTable();
            foreach (var item in lstColumns)
            {
                if (counter <= 6)
                    dt.Columns.Add(item, typeof(string));
                else if (counter == 7)
                    dt.Columns.Add(item, typeof(int));
                else if (counter == 8)
                    dt.Columns.Add(item, typeof(DateTime));
                else if (counter == 9)
                    dt.Columns.Add(item, typeof(string));               
                else
                    dt.Columns.Add(item, typeof(double));
 
                counter++;
            }
 
            dt.Columns.Add("OilWith1M", typeof(double));
            dt.Columns.Add("CapUtilPC", typeof(double));
            dt.Columns.Add("UnEmployement", typeof(double));
            dt.Columns.Add("NaturalGas", typeof(double));
            dt.Columns.Add("10YTreasury", typeof(double));
            dt.Columns.Add("USUnEmployemnet", typeof(double));
            dt.Columns.Add("IndustrialProduction", typeof(double));
            dt.Columns.Add("NewHomeSales", typeof(double));
            dt.Columns.Add("InventorySalesRatio", typeof(double));
            dt.Columns.Add("NaturalGasNYM", typeof(double));
 
            Random r = new Random();
            for (int i = 1; i < 500; i++)
            {
                DataRow row = dt.NewRow();
                counter = 0;
                foreach (var item in dt.Columns)
                {
                    if (counter <= 6)
                        row[item.ToString()] = Guid.NewGuid().ToString().Substring(0, 5);
                    else if (counter == 7)
                        row[item.ToString()] = r.Next(1000 * i);
                    else if (counter == 8)
                        row[item.ToString()] = DateTime.Now.AddDays(i);
                    else if (counter == 9)
                        row[item.ToString()] = Guid.NewGuid().ToString().Substring(0, 5);                   
                    else
                        row[item.ToString()] = r.Next(1000 * i);                   
 
                    counter++;
                }              
 
                dt.Rows.Add(row);
            }
 
            return dt;
        }
 
        private void radGridView1_Loaded(object sender, RoutedEventArgs e)
        {
            this.radGridView1.ItemsSource = LoadData();
        }
    }
}
Hi All,

I use RadGridView and i apply group by functionality, When the user groups by a particular column, could we adds row at bottom of each group (or it top header row) with average for the all the columns from R. Squared to the right.
(Its my grid column) 

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Sufyan
Top achievements
Rank 1
Share this question
or