
Dirk Devriendt
Top achievements
Rank 2
Dirk Devriendt
asked on 20 May 2007, 11:17 PM
Hi,
I have a requirement for subtotals on a gridview with databinding and grouping enabled (One extra row at the bottom of each grouped section with totals for all numerical fields).
I could of course adapt the DataTable, but it seemed more performant to me if I could use the GroupingChanged Event and just add the needed rows in the gridview.
But obviously, since I am writing this :-), I ran into some troubles trying to achieve this:
- The GroupingChanged event does not fire, even if it seems to be properly defined in the designer class
- I found out about the nested rows in the grouping structure and was able to get the code in place for calculating the subtotals row, but I cannot figure out how to add this new row without touching the underlying DataSet. Apparently it's just a bit more complicated than adding cells -and I am a bit confused about the relation between the row.Cells, row.rowInfo and row.Rows properties-.
- I will also be trying this for horizontal totals (1 per row), which is pretty easy to do on a data level, but if you have any remarks on how to do it on clientside, I am all ears :-)
I have a requirement for subtotals on a gridview with databinding and grouping enabled (One extra row at the bottom of each grouped section with totals for all numerical fields).
I could of course adapt the DataTable, but it seemed more performant to me if I could use the GroupingChanged Event and just add the needed rows in the gridview.
But obviously, since I am writing this :-), I ran into some troubles trying to achieve this:
- The GroupingChanged event does not fire, even if it seems to be properly defined in the designer class
- I found out about the nested rows in the grouping structure and was able to get the code in place for calculating the subtotals row, but I cannot figure out how to add this new row without touching the underlying DataSet. Apparently it's just a bit more complicated than adding cells -and I am a bit confused about the relation between the row.Cells, row.rowInfo and row.Rows properties-.
- I will also be trying this for horizontal totals (1 per row), which is pretty easy to do on a data level, but if you have any remarks on how to do it on clientside, I am all ears :-)
5 Answers, 1 is accepted
0
Hello Dirk,
Yes, you are right. GroupingChanged event does not fire and this is a problem we need to fix. We apologize for the inconvenience - expect this to be taken care of in our next service pack.
Currently we support subtotals only in group headers. To group the grid data by country and count the records in each group use this code:
this.radGridView1.MasterGridViewTemplate.AddGroupByExpression("Country, Count(Country) CNT Group By Country");
We will add support for custom footer rows in groups in next release.
GridViewRowInfo class is the logical representation of a single row.
GridViewRowInfo.Cells - This collection is used to access cell values (e.g):
string name = (string)this.radGridView1.MasterGridViewInfo.Rows[10].Cells["Name"].Value;
GridViewRowInfo.Rows - This collection represents child rows in a grouped RadGridView
Best wishes,
Jack
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Yes, you are right. GroupingChanged event does not fire and this is a problem we need to fix. We apologize for the inconvenience - expect this to be taken care of in our next service pack.
Currently we support subtotals only in group headers. To group the grid data by country and count the records in each group use this code:
this.radGridView1.MasterGridViewTemplate.AddGroupByExpression("Country, Count(Country) CNT Group By Country");
We will add support for custom footer rows in groups in next release.
GridViewRowInfo class is the logical representation of a single row.
GridViewRowInfo.Cells - This collection is used to access cell values (e.g):
string name = (string)this.radGridView1.MasterGridViewInfo.Rows[10].Cells["Name"].Value;
GridViewRowInfo.Rows - This collection represents child rows in a grouped RadGridView
Best wishes,
Jack
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
0

Dirk Devriendt
Top achievements
Rank 2
answered on 21 May 2007, 03:55 PM
Hi Jack,
Thanks for the detailed reply, I knew about the group header possibilities, but they do not really satisfy me since I (my users :-)) need the data to be aligned with the appropriate columns.
What was confusing about the GridViewRowInfo.Cells is that in the data viewer in VS2005 I only get a reference to the rowInfo and when trying to assign a value to a particular cell, I always get an index overflow error (both with column name and column index).
Other than that I also have difficulties adding a custom row; the code doesn't return any errors, but the row in question doesn't appear.
I include a bit of "proof of concept" code to clarify what I am trying to achieve: (the form contains a radgridview and a button for calculating the subtotals; drag the "Type" column to the GroupingBox and execute the button1_Click event)
Thanks for the detailed reply, I knew about the group header possibilities, but they do not really satisfy me since I (my users :-)) need the data to be aligned with the appropriate columns.
What was confusing about the GridViewRowInfo.Cells is that in the data viewer in VS2005 I only get a reference to the rowInfo and when trying to assign a value to a particular cell, I always get an index overflow error (both with column name and column index).
Other than that I also have difficulties adding a custom row; the code doesn't return any errors, but the row in question doesn't appear.
I include a bit of "proof of concept" code to clarify what I am trying to achieve: (the form contains a radgridview and a button for calculating the subtotals; drag the "Type" column to the GroupingBox and execute the button1_Click event)
private void Form1_Load(object sender, EventArgs e) |
{ |
DataTable dt = new DataTable(); |
dt.Columns.Add("Name"); |
dt.Columns.Add("Type"); |
dt.Columns.Add("JAN"); |
dt.Columns.Add("FEB"); |
dt.Columns.Add("MAR"); |
dt.Columns.Add("APR"); |
dt.Rows.Add(new object[] { "Satellite1", "GPS", 100, 200, 200, 300 }); |
dt.Rows.Add(new object[] { "Satellite2", "Galileo", 23, 232, 623, 33 }); |
dt.Rows.Add(new object[] { "Satellite3", "Galileo", 789, 768, 567, 23 }); |
dt.Rows.Add(new object[] { "Satellite4", "GPS", 45, 456, 89, 12 }); |
radGridView1.DataSource = dt.DefaultView; |
} |
private void button1_Click(object sender, EventArgs e) |
{ |
radGridView1.MasterGridViewTemplate.Columns.Insert(2, new GridViewTextBoxColumn("Total")); |
int columnNumber = radGridView1.MasterGridViewTemplate.Columns.Count; |
decimal value = 0; |
for (int r = 0; r < radGridView1.Rows.Count; r++) |
{ |
GridViewRowInfo headerRow = radGridView1.Rows[r]; |
decimal[] subTotals = new decimal[columnNumber]; |
if (headerRow.RowElementType.ToString() == "Telerik.WinControls.UI.GridGroupHeaderElement") |
{ |
foreach (GridViewRowInfo row in (headerRow.Rows)) |
{ |
decimal rowTotal = 0; |
for (int i = 3; i < columnNumber; i++) |
{ |
if (row.Cells[i].Value != null) |
{ |
if (decimal.TryParse(row.Cells[i].Value.ToString(), out value)) |
{ |
subTotals[i] += value; |
rowTotal += value; |
} |
} |
} |
row.Cells["Total"].Value = rowTotal.ToString(); |
} |
GridViewRowInfo newRow = new GridViewRowInfo(headerRow.GridViewInfo); |
newRow.Cells["Name"].Value = "Total"; |
for (int i = 3; i < columnNumber; i++) |
{ |
newRow.Cells[i].Value = subTotals[i]; |
} |
headerRow.Rows.Add(newRow); |
} |
} |
} |
0
Hi Dirk ,
Yes, I understand what your intention is. Unfortunately this is not possible with the current version of RadGridView. In this release we do not support adding unbound rows. We do support unbound columns however. To set the value of unbound cell element you must process the CellFormatting event (e.g):
radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
GridViewTextBoxColumn column = e.CellElement.Column as GridViewTextBoxColumn;
if (column != null && column.DataField == "Total")
{
e.CellElement.SetCellValue("my value");
}
}
I suggest you to use a hidden column and store all data in DataTable. To hide a column use this code:
this.radGridView1.MasterGridViewTemplate.Columns["Total"].IsVisible = false;
We will try to implement the functionality you requested in our next service pack. If you have any other requests or suggestions please tell us.
All the best,
Jack
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
Yes, I understand what your intention is. Unfortunately this is not possible with the current version of RadGridView. In this release we do not support adding unbound rows. We do support unbound columns however. To set the value of unbound cell element you must process the CellFormatting event (e.g):
radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
GridViewTextBoxColumn column = e.CellElement.Column as GridViewTextBoxColumn;
if (column != null && column.DataField == "Total")
{
e.CellElement.SetCellValue("my value");
}
}
I suggest you to use a hidden column and store all data in DataTable. To hide a column use this code:
this.radGridView1.MasterGridViewTemplate.Columns["Total"].IsVisible = false;
We will try to implement the functionality you requested in our next service pack. If you have any other requests or suggestions please tell us.
All the best,
Jack
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
0

Dirk Devriendt
Top achievements
Rank 2
answered on 22 May 2007, 10:27 PM
Hi Jack,
Excellent info, thanks, I was afraid it wouldn't be possible. For those who do seek a similar solution; it is obviously an option to add the "total" rows in the datatable and fill its values, but it obviously is a less than optimal solution...
To move things forward, I have now been forced to start on a bunch of Excel macros; can you imagine my horror :-)
and since you ask; my requests:
- multiple selection on cell level
- split screen
- stretching it though really handy sometimes: updateable pivot
Thanks again,
Dirk
Excellent info, thanks, I was afraid it wouldn't be possible. For those who do seek a similar solution; it is obviously an option to add the "total" rows in the datatable and fill its values, but it obviously is a less than optimal solution...
To move things forward, I have now been forced to start on a bunch of Excel macros; can you imagine my horror :-)
and since you ask; my requests:
- multiple selection on cell level
- split screen
- stretching it though really handy sometimes: updateable pivot
Thanks again,
Dirk
0
Hello Dirk,
You are right. The optimal way to do this is to access directly the grid rows and cells. We will include unbound mode support in one of our next versions. Thank you for your suggestions, much appreciated.
As to multiple selection, it is planed for our next Q. We will put split view and pivot grid in our TODO list as well.
All the best,
Jack
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
You are right. The optimal way to do this is to access directly the grid rows and cells. We will include unbound mode support in one of our next versions. Thank you for your suggestions, much appreciated.
As to multiple selection, it is planed for our next Q. We will put split view and pivot grid in our TODO list as well.
All the best,
Jack
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center