Group Totals in SPGridView

3.3k Views Asked by At

When I group items in a SPGridView using the GroupField property the grouping works great but the total row count does not show up like in a standard SharePoint list. Usually SharePoint will display the count next to the group in parenthesis (i.e. Group 1 (5)). Is there a way to enable this functionality.

5

There are 5 best solutions below

0
On

I know this question is old, but anyway:

var rows = document.getElementById("your_table").getElementsByTagName("TR");
for (var i = 0; i < rows.length; i++) {
    if (rows[i].className == 'ms-gb') {
        var count = 0;
        var el = rows[i].nextSibling;
        while (el) {
            if (el.className != 'ms-gb')
                count++;
            else
                break;
            el = el.nextSibling;
        }
        rows[i].children[0].innerHTML += " <span dir='ltr'>(" + count + ")</span>";
       }
    }

You can drop.getElementById("your_table") part if you want to apply it to the whole page anyway, otherwise put the gridview in a control with that ID.

1
On

have you tried setting the DisplayGroupFieldName property to True?

SPGridView class documentation

0
On

I had a quick look at the code and even overriding the SPGridview does not seem to have an easy "hook" to override for this.

You may be able to create Javascript to do this, but it would be far from entertaining.

0
On

Javascript works! Nat advice helped me!

See jquery example below:

$(".ms-gb td:contains('" + groupName + "')")
    .append("<div class='stat'>text</div>")
0
On

If you don't want to use javascript, you could iterate through the table and count the rows in each group.

The downside is that you would probably need to count all of the rows and then go back and update the group name in each row.

For example:

ID  Value   Group
1   One     GroupA
2   Two     GroupA
3   Three   GroupB
4   Four    GroupB
5   Five    GroupB

2 in GroupA
3 in GroupB

ID  Value   Group
1   One     GroupA (2)
2   Two     GroupA (2)
3   Three   GroupB (3)
4   Four    GroupB (3)
5   Five    GroupB (3)