This is a migrated thread and some comments may be shown as answers.

Aggregate Sum on boolean column

6 Answers 124 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Darren
Top achievements
Rank 1
Darren asked on 20 Apr 2009, 09:34 PM
I have a column in the grid that is mapped to a sql.bit type field and I am wondering how I can somehow sum the column using  Aggregate='Sum'.

Basically, when the column has 'true' in it I would like to sum (or count) these occurances and have it display in the footer (in this same column) and also in the group footer. Is it possible to keep the 'true' in the column but change it to 1 for sum'ing purposes only?

Thanks in advance.

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 22 Apr 2009, 09:43 AM
Hi Darren,

Try the following code snippet for showing the total number of checked items in the grid footer.

ASPX:
 
<telerik:GridCheckBoxColumn DataField="MartalStatus" HeaderText="MartalStatus" SortExpression="MartalStatus" UniqueName="MartalStatus" DataType="System.Boolean"
</telerik:GridCheckBoxColumn> 

CS:
 
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
    if (e.Item is GridDataItem) 
    { 
        GridDataItem dataItem = e.Item as GridDataItem; 
        CheckBox chk = (CheckBox)dataItem["MartalStatus"].Controls[0]; 
        if (chk.Checked) 
        { 
            total++; 
        } 
    } 
    if (e.Item is GridFooterItem) 
    { 
        GridFooterItem footerItem = e.Item as GridFooterItem; 
        footerItem["MartalStatus"].Text = "Total Checked Items: " + total.ToString(); 
    } 

Thanks,
Shinu.
0
Cyrus
Top achievements
Rank 1
answered on 28 Aug 2009, 10:50 PM
This code doesn't appear to be complete.  You have not declared the variable total and there doesn't appear to be a looping mechanism to add everything up?  Perhaps it is different in VB but I tried to convert your code and it doesn't do the trick.
0
Cyrus
Top achievements
Rank 1
answered on 02 Sep 2009, 03:03 PM
Could someone please update this code?  It would be REALLY handy if you added an aggragate option to checkbox columns...yes some people actually want totals for this kind of field.
0
Sebastian
Telerik team
answered on 02 Sep 2009, 04:00 PM

Hi Cyrus,

You can find another code example in the second paragraph of this help topic:
http://www.telerik.com/help/aspnet-ajax/grdtotalsingridfooters.html (section "Programmatic solution")

The idea is to detect the checked state of the checkbox inside the GridCheckBoxColumn and increase the total that will be displayed in the footer.

Best regards,

Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Cyrus
Top achievements
Rank 1
answered on 02 Sep 2009, 05:16 PM
Ahhh it was the scope of my variable.... Shinu didn't show the variable declaration in the code sample :)  I knew it was something easy.
0
Danyell
Top achievements
Rank 1
answered on 14 Mar 2014, 06:42 AM
Hi Darren,

Shinu provided a way to accomplish this with back-end code.  I am suggesting an Expression that can be used in the GUI.  Set the value of a text box to the Expression below, and add it to the relevant grouping.

= Sum(IIf(Fields.IsRejected="True",1,0))

The if statement reads, if {your field name} equals True, change the value to 1, otherwise change to 0.  From there the Sum function adds up the 1s.

Hope this helps others!
Tags
Grid
Asked by
Darren
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Cyrus
Top achievements
Rank 1
Sebastian
Telerik team
Danyell
Top achievements
Rank 1
Share this question
or