T
Tincho
I have the following problem
I have a datagrid and I want to add a footer to it, containing totals
for some of the columns.
I want the totals to show only the totalized columns, something like
A B C D E F G
+-------------------------------------------------------------------+
| | | 2 | 5 | | | |
| | | 3 | 6 | | | |
| | | 4 | 7 | | | |
| | | 5 | 8 | | | |
| | | 6 | 9 | | | |
| | | 7 | 0 | | | |
+-------------------------------------------------------------------+
| Total | 27 | 35 | |
+-------------------------------------------------------------------+
I got to do this. I would handle the datagrid's ItemDataBound and
would get it to work. It showed just as I posted.
The problem came when I needed to make some columns of the datagrid
invisible.
I don't care about the colspan for now, but the problem I'm having is
that the items in the footer are not showing up.
The total item of the footer shows up and the 27 number also, but the
35 dissapears. The datagrid does not render it, even though that while
debugging after the databind the footer contains 3 items , it only
shows 2.
Any ideas?
Thanks in advance
Tincho
PS: For what I've seen until now, it depends on how many columns are
invisible, but still trying to figure what happens.
PS 2: Here's the method I'm using for the ItemDataBound event.
Try using it in any grid you want and will do what I described. After
that try making some columns invisible and you will get the problem
private void dgDocumentAnalysis_ItemDataBound(object sender,
DataGridItemEventArgs e) {
if( e.Item.ItemType == ListItemType.Footer ) {
ArrayList columns = new ArrayList(
this.GetColumnsWithTotals() );
columns.Sort();
e.Item.Cells[0].Text = "Total";
int firstColumnOfSpan = 0;
int firstVisibleColumnOfSpan = 0;
int visibleColumnNumber = 0;
for( int i = 0, footerIndex = 0, previousVisibleColumnNumber
= 0; i < this.dgDocumentAnalysis.Columns.Count; i++ ) {
// The column must show it's total
if( columns.Contains( visibleColumnNumber ) ||
footerIndex == 0 ) {
if( footerIndex == 0 ) {
e.Item.Cells[0].Text = "Total";
}
else {
e.Item.Cells[ footerIndex - 1 ].ColumnSpan =
visibleColumnNumber - previousVisibleColumnNumber;
previousVisibleColumnNumber =
visibleColumnNumber;
e.Item.Cells[ footerIndex ].Text =
footerIndex.ToString();
e.Item.Cells[ footerIndex ].ColumnSpan = 1;
}
footerIndex++;
} else {
e.Item.Cells.RemoveAt( footerIndex );
}
if( this.dgDocumentAnalysis.Columns.Visible )
visibleColumnNumber ++;
}
}
}
I have a datagrid and I want to add a footer to it, containing totals
for some of the columns.
I want the totals to show only the totalized columns, something like
A B C D E F G
+-------------------------------------------------------------------+
| | | 2 | 5 | | | |
| | | 3 | 6 | | | |
| | | 4 | 7 | | | |
| | | 5 | 8 | | | |
| | | 6 | 9 | | | |
| | | 7 | 0 | | | |
+-------------------------------------------------------------------+
| Total | 27 | 35 | |
+-------------------------------------------------------------------+
I got to do this. I would handle the datagrid's ItemDataBound and
would get it to work. It showed just as I posted.
The problem came when I needed to make some columns of the datagrid
invisible.
I don't care about the colspan for now, but the problem I'm having is
that the items in the footer are not showing up.
The total item of the footer shows up and the 27 number also, but the
35 dissapears. The datagrid does not render it, even though that while
debugging after the databind the footer contains 3 items , it only
shows 2.
Any ideas?
Thanks in advance
Tincho
PS: For what I've seen until now, it depends on how many columns are
invisible, but still trying to figure what happens.
PS 2: Here's the method I'm using for the ItemDataBound event.
Try using it in any grid you want and will do what I described. After
that try making some columns invisible and you will get the problem
private void dgDocumentAnalysis_ItemDataBound(object sender,
DataGridItemEventArgs e) {
if( e.Item.ItemType == ListItemType.Footer ) {
ArrayList columns = new ArrayList(
this.GetColumnsWithTotals() );
columns.Sort();
e.Item.Cells[0].Text = "Total";
int firstColumnOfSpan = 0;
int firstVisibleColumnOfSpan = 0;
int visibleColumnNumber = 0;
for( int i = 0, footerIndex = 0, previousVisibleColumnNumber
= 0; i < this.dgDocumentAnalysis.Columns.Count; i++ ) {
// The column must show it's total
if( columns.Contains( visibleColumnNumber ) ||
footerIndex == 0 ) {
if( footerIndex == 0 ) {
e.Item.Cells[0].Text = "Total";
}
else {
e.Item.Cells[ footerIndex - 1 ].ColumnSpan =
visibleColumnNumber - previousVisibleColumnNumber;
previousVisibleColumnNumber =
visibleColumnNumber;
e.Item.Cells[ footerIndex ].Text =
footerIndex.ToString();
e.Item.Cells[ footerIndex ].ColumnSpan = 1;
}
footerIndex++;
} else {
e.Item.Cells.RemoveAt( footerIndex );
}
if( this.dgDocumentAnalysis.Columns.Visible )
visibleColumnNumber ++;
}
}
}