Solved

Multiple Calculations in Gridview Footer

Posted on 2013-02-07
5
602 Views
Last Modified: 2013-02-18
I am using VS 2010 and C# asp.net 4.0. In the footer, I need to display 5 lines of summary information based on all the rows in the gridview.
Average
Minimum
Maximum
Range = Maximum-Minimum
Standard Deviation

I have the average calculating correctly and displaying correctly on the gridview footer. I'm including my sql selection and RowDataBound code. There may be an easier way to do this than I started, but this at least works. What I need to know how to do is get the minimum, maximum and std deviation calculated in the footer. My variables are all declared and set to 0 in the partial class.

      
            if (RadioButtonList1.SelectedIndex == 1)
            {
                SqlDataSource1.SelectParameters.Add(new Parameter("sample", System.TypeCode.String, DropDownList1.SelectedValue));
                SqlDataSource1.SelectParameters.Add(new Parameter("startdate", System.TypeCode.DateTime, txtStartDate.Text));
                SqlDataSource1.SelectParameters.Add(new Parameter("enddate", System.TypeCode.DateTime, txtEndDate.Text));
                SqlDataSource1.SelectCommand = "SELECT * FROM [VW_FIBER_DATA_ANALYSIS] WHERE [SAMPLE_DATE] BETWEEN @startdate AND @enddate AND SAMPLE_TYPE=@sample ORDER BY [SAMPLE_DATE]";
                GridView1.DataBind();
                GridView1.Visible = true;
            }


  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (DataBinder.Eval(e.Row.DataItem, "MOISTURE") != System.DBNull.Value)
                {
                    sumMoisture += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "MOISTURE"));
                    countMoisture += 1;
                }

                if (DataBinder.Eval(e.Row.DataItem, "10_MESH_%") != System.DBNull.Value)
                {
                    sum10mesh += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "10_MESH_%"));
                    count10mesh += 1;
                }

                if (DataBinder.Eval(e.Row.DataItem, "20_MESH_%") != System.DBNull.Value)
                {
                    sum20mesh += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "20_MESH_%"));
                    count20mesh += 1;
                }

                if (DataBinder.Eval(e.Row.DataItem, "40_MESH_%") != System.DBNull.Value)
                {
                    sum40mesh += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "40_MESH_%"));
                    count40mesh += 1;
                }

                if (DataBinder.Eval(e.Row.DataItem, "60_MESH_%") != System.DBNull.Value)
                {
                    sum60mesh += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "60_MESH_%"));
                    count60mesh += 1;
                }

                if (DataBinder.Eval(e.Row.DataItem, "80_MESH_%") != System.DBNull.Value)
                {
                    sum80mesh += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "80_MESH_%"));
                    count80mesh += 1;
                }

                if (DataBinder.Eval(e.Row.DataItem, "100_MESH_%") != System.DBNull.Value)
                {
                    sum100mesh += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "100_MESH_%"));
                    count100mesh += 1;
                }

                if (DataBinder.Eval(e.Row.DataItem, "200_MESH_%") != System.DBNull.Value)
                {
                    sum200mesh += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "200_MESH_%"));
                    count200mesh += 1;
                }

                if (DataBinder.Eval(e.Row.DataItem, "PAN_MESH_%") != System.DBNull.Value)
                {
                    sumPanmesh += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "PAN_MESH_%"));
                    countPanmesh += 1;
                }

                if (DataBinder.Eval(e.Row.DataItem, "PERCENT_RECOVERY") != System.DBNull.Value)
                {
                    sumPctRec += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "PERCENT_RECOVERY"));
                    countPctRec += 1;
                }

            }

            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                if (countMoisture != 0)
                {
                    avgMoisture = sumMoisture / countMoisture;
                    maxMoisture = 0; 
                    minMoisture = 0;
                    rangeMoisture = maxMoisture-minMoisture;
                    stdevMoisture = 0;
                }

                if (count10mesh != 0)
                {
                    avg10mesh = sum10mesh / count10mesh;
                    max10mesh = 0;
                    min10mesh = 0;
                    range10mesh = max10mesh-min10mesh;
                    stdev10mesh = 0;
                }

                if (count20mesh != 0)
                {
                    avg20mesh = sum20mesh / count20mesh;
                    max20mesh = 0;
                    min20mesh = 0;
                    range20mesh = max20mesh-min20mesh;
                    stdev20mesh = 0;
                }

                if (count40mesh != 0)
                {
                    avg40mesh = sum40mesh / count40mesh;
                    max40mesh = 0;
                    min40mesh = 0;
                    range40mesh = max40mesh-min40mesh;
                    stdev40mesh = 0;
                }

                if (count60mesh != 0)
                {
                    avg60mesh = sum60mesh / count60mesh;
                    max60mesh = 0;
                    min60mesh = 0;
                    range60mesh = max60mesh-min60mesh;
                    stdev60mesh = 0;
                }

                if (count80mesh != 0)
                {
                    avg80mesh = sum80mesh / count80mesh;
                    max80mesh = 0;
                    min80mesh = 0;
                    range80mesh = max80mesh-min80mesh;
                    stdev80mesh = 0;
                }

                if (count100mesh != 0)
                {
                    avg100mesh = sum100mesh / count100mesh;
                    max100mesh = 0;
                    min100mesh = 0;
                    range100mesh = max100mesh-min100mesh;
                    stdev100mesh = 0;
                }

                if (count200mesh != 0)
                {
                    avg200mesh = sum200mesh / count200mesh;
                    max200mesh = 0;
                    min200mesh = 0;
                    range200mesh = max200mesh-min200mesh;
                    stdev200mesh = 0;
                }

                if (countPanmesh != 0)
                {
                    avgPanmesh = sumPanmesh / countPanmesh;
                    maxPanmesh = 0;
                    minPanmesh = 0;
                    rangePanmesh = maxPanmesh-minPanmesh;
                    stdevPanmesh = 0;
                }

                if (countPctRec != 0)
                {
                    avgPctRec = sumPctRec / countPctRec;
                    maxPctRec = 0;
                    minPctRec = 0;
                    rangePctRec = maxPctRec-minPctRec;
                    stdevPctRec = 0;
                }

                e.Row.Cells[9].Text = "Average:" + "<br />Maximum:" + "<br />Minimum:" + "<br />Range:" + "<br />Std. Dev.:";

                e.Row.Cells[10].Text = avgMoisture.ToString("##0.0") + "<br />" + maxMoisture.ToString("##0.0") + "<br />" + minMoisture.ToString("##0.0") + "<br />" + rangeMoisture.ToString("##0.0") + "<br />" + stdevMoisture.ToString("##0.0");

                e.Row.Cells[11].Text = avg10mesh.ToString("##0.0") + "<br />" + max10mesh.ToString("##0.0") + "<br />" + min10mesh.ToString("##0.0") + "<br />" + range10mesh.ToString("##0.0") + "<br />" + stdev10mesh.ToString("##0.0");

                e.Row.Cells[12].Text = avg20mesh.ToString("##0.0") + "<br />" + max20mesh.ToString("##0.0") + "<br />" + min20mesh.ToString("##0.0") + "<br />" + range20mesh.ToString("##0.0") + "<br />" + stdev20mesh.ToString("##0.0");

                e.Row.Cells[13].Text = avg40mesh.ToString("##0.0") + "<br />" + max40mesh.ToString("##0.0") + "<br />" + min40mesh.ToString("##0.0") + "<br />" + range40mesh.ToString("##0.0") + "<br />" + stdev40mesh.ToString("##0.0");

                e.Row.Cells[14].Text = avg60mesh.ToString("##0.0") + "<br />" + max60mesh.ToString("##0.0") + "<br />" + min60mesh.ToString("##0.0") + "<br />" + range60mesh.ToString("##0.0") + "<br />" + stdev60mesh.ToString("##0.0");

                e.Row.Cells[15].Text = avg80mesh.ToString("##0.0") + "<br />" + max80mesh.ToString("##0.0") + "<br />" + min80mesh.ToString("##0.0") + "<br />" + range80mesh.ToString("##0.0") + "<br />" + stdev80mesh.ToString("##0.0");

                e.Row.Cells[16].Text = avg100mesh.ToString("##0.0") + "<br />" + max100mesh.ToString("##0.0") + "<br />" + min100mesh.ToString("##0.0") + "<br />" + range100mesh.ToString("##0.0") + "<br />" + stdev100mesh.ToString("##0.0");

                e.Row.Cells[17].Text = avg200mesh.ToString("##0.0") + "<br />" + max200mesh.ToString("##0.0") + "<br />" + min200mesh.ToString("##0.0") + "<br />" + range200mesh.ToString("##0.0") + "<br />" + stdev200mesh.ToString("##0.0");

                e.Row.Cells[18].Text = avgPanmesh.ToString("##0.0") + "<br />" + maxPanmesh.ToString("##0.0") + "<br />" + minPanmesh.ToString("##0.0") + "<br />" + rangePanmesh.ToString("##0.0") + "<br />" + stdevPanmesh.ToString("##0.0");

                e.Row.Cells[19].Text = avgPctRec.ToString("###0.0") + "<br />" + maxPctRec.ToString("##0.0") + "<br />" + minPctRec.ToString("##0.0") + "<br />" + rangePctRec.ToString("##0.0") + "<br />" + stdevPctRec.ToString("##0.0");
            }

        }

Open in new window


I can get the range easily once I figure out how to get the minimum and maximum. Thanks in advance for any help you can provide.
screenshot.png
0
Comment
Question by:Hers2keep
  • 4
5 Comments
 

Author Comment

by:Hers2keep
ID: 38865871
Well, I was able to get the minimum and maximum by using this code in the RowDataBound event.

                    
if (minMoisture == 0 && Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "MOISTURE")) > 0 || Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "MOISTURE")) < minMoisture)
                    {
                        minMoisture = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "MOISTURE"));
                    }

                    if (Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "MOISTURE")) > maxMoisture)
                    {
                        maxMoisture = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "MOISTURE"));
                    }

Open in new window


I'm still working on the standard deviation piece.
0
 

Author Comment

by:Hers2keep
ID: 38868823
I have not yet figured out how to get the standard deviation calculation into the footer of the GridView. Does anyone have any ideas on how to accomplish that?
0
 
LVL 12

Assisted Solution

by:jagssidurala
jagssidurala earned 500 total points
ID: 38872380
We cannot achive std deviation through row databound as it is depends the calculated values with  in the grid.

so take one label out side the grid, using for each you can derive this. foreach is faster so no issues to implement std deviation with foreach.

in for each you have to find each individual sample value by databinder like Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "MOISTURE")).

follow steps explained in below Url in foreach.

http://standard-deviation.appspot.com/
0
 

Accepted Solution

by:
Hers2keep earned 0 total points
ID: 38886809
I ended up using two gridviews and lining them up and doing the summary calculations using a view in SQL. It's working like a charm now.
0
 

Author Closing Comment

by:Hers2keep
ID: 38900975
I was not able to get the other solution doing what I needed to do. Since jagssidurala was the only one who responded at all, I wanted to give him the points.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PowerShell:  Column widths won't expand 3 49
JQuery Detecting page post back 6 19
C# Windows Form Navigation - Total Beginner 9 40
Web page design problem 3 13
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question