Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multiple Calculations in Gridview Footer

Posted on 2013-02-07
5
Medium Priority
?
620 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:Carla Romere
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 

Author Comment

by:Carla Romere
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:Carla Romere
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 1500 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:
Carla Romere 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:Carla Romere
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 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