I have three defined columns and then a collection of dynamic columns like below.
The basic purpose is to create defined columns called "Category", "Class", and "Measure".
Then create a location column A, a location column B, a column to show the difference between A and B,
a location column C, a column to show the difference between A and C, etc for an arbitrary number of
columns.
The code used to accomplish this:
gv.AutoGenerateColumns =
false
;
GridViewDataColumn catColumn =
new
GridViewDataColumn();
catColumn.DataMemberBinding =
new
Binding(
"Category"
);
catColumn.Header =
"Category"
;
gv.Columns.Add(catColumn);
GridViewDataColumn clsColumn =
new
GridViewDataColumn();
clsColumn.DataMemberBinding =
new
Binding(
"Class"
);
catColumn.Header =
"Category"
;
gv.Columns.Add(clsColumn);
GridViewDataColumn msrColumn =
new
GridViewDataColumn();
msrColumn.DataMemberBinding =
new
Binding(
"Measure"
);
msrColumn.Header =
"Measure"
;
gv.Columns.Add(msrColumn);
int
counter = 0;
foreach
(DataWarehouseDistrict.ViewDistrict aDistrict
in
theReportData)
{
numColumns++;
GridViewDataColumn locColumn =
new
GridViewDataColumn();
locColumn.IsSortable =
true
;
locColumn.Header = aDistrict.DSTR_NM.ToString();
gv.Columns.Add(locColumn);
if
(counter >= 1)
{
GridViewDataColumn difColumn =
new
GridViewDataColumn();
difColumn.IsSortable =
true
;
difColumn.Header = aDistrict.DSTR_NM +
" Difference"
;
gv.Columns.Add(difColumn);
}
counter++;
}
Then in the RowLoaded event handler, I walk each row to place values for each location column out of a
collection of measures. An example of this in code (not the complete algorithm but enough to get an
idea):
private
void
gvWhatIfAnalysis_RowLoaded(
object
sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
MyMeasureClass theMeasure = measureCollection[0];
// list of theMeasure objects
switch
(theMeasure.AttributeName.ToString())
{
case
"Measure 1"
:
int
locationCounter = 0;
foreach
(theReports aReport
in
reportCollection)
// list of reports for locations with values for the measures
{
if
((locationCounter != 0) && (locationCounter % 2 == 0))
// even locations are diff calculations and 0 is Base Location
{
location = (TextBlock)e.Row.Cells[(3 + locationCounter) - 1].Content;
locationMeasureValue = location.Text;
baseLocation = (TextBlock)e.Row.Cells[3].Content;
baseLocationMeasureValue = baseLocation.Text;
if
(baseLocationMeasureValue == 0)
{
diff = 0;
}
else
{
diff = (locationMeasureValue - baseLocationMeasureValue) / baseLocationMeasureValue;
}
diffDisplay = (TextBlock)e.Row.Cells[3 + locationCounter].Content;
diffDisplay.Text = String.Format(
"{0:P2}"
, diff);
locationCounter++;
}
TextBlock location = (TextBlock)e.Row.Cells[3 + locationCounter].Content;
location.Text = String.Format(
"{0:N2}"
, aReport.Measure1);
// matches the measure store in the param that equals the switch case
locationCounter++;
}
break
;
Unfortunately I cannot set the DataMemberBinding of the dynamically bound columns because I do not know
if I will have 2 or 200 locations. There is no way to model that in a finite class to attach to the
GridView's ItemsSource. I do have the GridView's ItemsSource attached to a class that defines values
for the first three columns (Category, Class and Measure).
What I need to be able to do is sort the location columns. I have tried setting a SortDescriptor and
setting the Member name equal to the name of location for the dynamic columns but that does not work.
Is there a way to sort these dynamic columns?
Thanks much.
Chris