Home All Groups Group Topic Archive Search About

Datagrid Footer now showing up correctly

Author
9 Feb 2005 3:19 PM
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[i].Visible )
                    visibleColumnNumber ++;
          }
     }
}

Author
10 Feb 2005 6:36 PM
Alvin Bruney [MVP]
how are you disabling the columns?

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
----------------------------------------------------------


Show quoteHide quote
"Tincho" <tinc***@gmail.com> wrote in message
news:de0d8c93.0502090719.d3d7ba@posting.google.com...
>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[i].Visible )
>                    visibleColumnNumber ++;
>          }
>     }
> }