I am using a RadGridView whose ItemsSource is bound to a DataTable.
The reason for using DataTable is because the columns have to be generated dynamically based on user's selection.
I've added Aggregate Functions to each columns and recalculate them when the data source changes.
The RadGridView throws exceptions on certain conditions.
I've attached a sample application that reproduces the error.
the application is contrived from the original application to include only the necessary part.
(The original application uses MVVM pattern, and the recalculation of aggregates happens when the ItemsSource in the ViewModel changes.)
there are 2 different errors that I've encountered so far.
here are the steps to reproduce the error.
Error A
1. set the 'number of data columns' at the top right corner to 2(default), click the 'Load' button 3 times.
2. at the 3rd click, Telerik.Windows.Controls.GridView.GridViewFooterCell.AggregateResults_CollectionChanged throws exception.
Error B
1. set 'Add Count Function to Time column' to true, 'number of data columns' to 2, then click the 'Load' button
2. set 'number of data columns' to 1, then click the 'Load' button twice.
3. at the 2nd click of Step 2, RadGridView.CalculateAggregates() throws exception.
hope you look into this error,
and if you have a better solution on what I'm trying to achieve(dynamic column generation, calculate aggregates),
please do suggest.
Thanks.
I've discovered that you can't attach solution files in this forum, so I'll put the code below.
Main.xaml
Main.xaml.cs
The reason for using DataTable is because the columns have to be generated dynamically based on user's selection.
I've added Aggregate Functions to each columns and recalculate them when the data source changes.
The RadGridView throws exceptions on certain conditions.
I've attached a sample application that reproduces the error.
the application is contrived from the original application to include only the necessary part.
(The original application uses MVVM pattern, and the recalculation of aggregates happens when the ItemsSource in the ViewModel changes.)
there are 2 different errors that I've encountered so far.
here are the steps to reproduce the error.
Error A
1. set the 'number of data columns' at the top right corner to 2(default), click the 'Load' button 3 times.
2. at the 3rd click, Telerik.Windows.Controls.GridView.GridViewFooterCell.AggregateResults_CollectionChanged throws exception.
Error B
1. set 'Add Count Function to Time column' to true, 'number of data columns' to 2, then click the 'Load' button
2. set 'number of data columns' to 1, then click the 'Load' button twice.
3. at the 2nd click of Step 2, RadGridView.CalculateAggregates() throws exception.
hope you look into this error,
and if you have a better solution on what I'm trying to achieve(dynamic column generation, calculate aggregates),
please do suggest.
Thanks.
I've discovered that you can't attach solution files in this forum, so I'll put the code below.
Main.xaml
<
Window
x:Class
=
"RadGridViewAggregateFunctionTest.MainView"
xmlns:telerik
=
"http://schemas.telerik.com/2008/xaml/presentation"
Title
=
"MainWindow"
Height
=
"350"
Width
=
"600"
>
<
Grid
x:Name
=
"grid"
>
<
Grid.RowDefinitions
>
<
RowDefinition
Height
=
"auto"
/>
<
RowDefinition
/>
</
Grid.RowDefinitions
>
<
StackPanel
Orientation
=
"Horizontal"
>
<
TextBlock
Text
=
"number of data columns : "
VerticalAlignment
=
"Center"
/>
<
telerik:RadNumericUpDown
x:Name
=
"radNumericUpDown"
IsInteger
=
"True"
ValueChanged
=
"radNumericUpDown_ValueChanged"
Value
=
"2"
/>
<
CheckBox
Content
=
"Add Count Function at Time column"
VerticalAlignment
=
"Center"
Margin
=
"20,0,0,0"
Checked
=
"CheckBox_Checked"
/>
<
Button
x:Name
=
"LoadButton"
Grid.Row
=
"0"
Width
=
"100"
Content
=
"Load"
Margin
=
"20,0,0,0"
Click
=
"LoadButton_Click"
/>
</
StackPanel
>
<
telerik:RadGridView
x:Name
=
"radGridView"
Grid.Row
=
"1"
ItemsSource
=
"{Binding}"
ShowColumnFooters
=
"True"
ShowGroupPanel
=
"False"
>
</
telerik:RadGridView
>
</
Grid
>
</
Window
>
Main.xaml.cs
using
System;
using
System.Linq;
using
System.Data;
using
System.Windows;
using
Telerik.Windows.Controls;
using
Telerik.Windows.Data;
using
System.IO;
using
System.Windows.Controls;
namespace
RadGridViewAggregateFunctionTest
{
public
partial
class
MainView : Window
{
Random random =
new
Random();
private
const
int
TIME_COLUMN_INDEX = 0;
int
numberOfColumns = 0;
bool
shouldAddCountFunctionAtTimeColumn =
false
;
public
MainView()
{
InitializeComponent();
}
private
void
LoadButton_Click(
object
sender, RoutedEventArgs e)
{
radGridView.DataContext = GenerateTable();
AddAggregateFunctions(radGridView);
}
private
void
AddAggregateFunctions(RadGridView gridView)
{
foreach
(var column
in
gridView.Columns)
{
if
(column.DisplayIndex == TIME_COLUMN_INDEX)
{
if
(shouldAddCountFunctionAtTimeColumn)
{
CountFunction countFunction =
new
CountFunction();
countFunction.Caption =
"Count : "
;
column.AggregateFunctions.Add(countFunction);
}
continue
;
}
MinFunction minFunction =
new
MinFunction();
minFunction.Caption =
"Min : "
;
column.AggregateFunctions.Add(minFunction);
MaxFunction maxFunction =
new
MaxFunction();
maxFunction.Caption =
"Max : "
;
column.AggregateFunctions.Add(maxFunction);
AverageFunction averageFunction =
new
AverageFunction();
averageFunction.Caption =
"Average : "
;
column.AggregateFunctions.Add(averageFunction);
}
gridView.CalculateAggregates();
}
private
DataTable GenerateTable()
{
DataTable table =
new
DataTable(
"My Table"
);
GenerateColumns(table);
GenerateRows(table);
return
table;
}
private
void
GenerateColumns(DataTable table)
{
DataColumn column =
new
DataColumn(
"Time"
,
typeof
(DateTime));
table.Columns.Add(column);
for
(
int
i = 0; i < numberOfColumns; i++)
{
string
columnName = Path.GetRandomFileName().Substring(0, 4);
column =
new
DataColumn(columnName,
typeof
(
float
));
table.Columns.Add(column);
}
}
private
void
GenerateRows(DataTable table)
{
DataRow row;
for
(
int
i = 0; i < 20; i++)
{
row = table.NewRow();
row[
"Time"
] = DateTime.Now.AddSeconds(i);
foreach
(DataColumn column
in
table.Columns)
{
if
(column.ColumnName ==
"Time"
)
{
continue
;
}
row[column.ColumnName] = (
float
)random.NextDouble() * 50;
}
table.Rows.Add(row);
}
}
private
void
radNumericUpDown_ValueChanged(
object
sender, RadRangeBaseValueChangedEventArgs e)
{
Int32.TryParse(radNumericUpDown.Value.ToString(),
out
numberOfColumns);
}
private
void
CheckBox_Checked(
object
sender, RoutedEventArgs e)
{
shouldAddCountFunctionAtTimeColumn = ((CheckBox)sender).IsChecked.Value;
}
}
}