Datagrid Footer now showing up correctly

Discussion in 'ASP .Net Datagrid Control' started by Tincho, Feb 9, 2005.

  1. Tincho

    Tincho Guest

    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 ++;
    }
    }
    }
    Tincho, Feb 9, 2005
    #1
    1. Advertising

  2. 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
    ----------------------------------------------------------


    "Tincho" <> wrote in message
    news:...
    >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 ++;
    > }
    > }
    > }
    Alvin Bruney [MVP], Feb 10, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVzdGlu?=

    Showing datagrid totals in the footer

    =?Utf-8?B?SnVzdGlu?=, Oct 14, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    4,565
    =?Utf-8?B?SnVzdGlu?=
    Oct 15, 2004
  2. Ray Booysen
    Replies:
    2
    Views:
    631
    Ray Booysen
    Mar 28, 2006
  3. Stu Lock
    Replies:
    1
    Views:
    20,440
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 19, 2006
  4. Fister
    Replies:
    7
    Views:
    4,133
    Bergamot
    Jan 13, 2008
  5. Henrik
    Replies:
    1
    Views:
    416
    Ken Cox [Microsoft MVP]
    Jul 6, 2006
Loading...

Share This Page