Link to home
Start Free TrialLog in
Avatar of Carla Romere
Carla RomereFlag for United States of America

asked on

Multiple Calculations in Gridview Footer

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
Avatar of Carla Romere
Carla Romere
Flag of United States of America image

ASKER

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.
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?
SOLUTION
Avatar of jagssidurala
jagssidurala
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.