0

I have a gridview that has similar to this

  Hour - ActualA - TargetA - ActualB - TargetB - ActualC - TargetC - ActualAA 
    1        7         10        9         12        0         17         21

note - The targets stay the same in each column.

ETC ETC going to 8 hours... with different figures throughout the grid...

I have a label that sums all the actuals and displays this in a label control, this works fine as it sums all the actual figures and gets the total..

What i need to do is sum each hour and variant and say if actual is more than target then label is green, if less than then the label is red.. But i am not sure how i can achieve this, this is my code so far but it doesnt work... maybe i am overcomplicating things? can anyone help...

 public int CalculatePackingTotal(DataTable dt)
    {
        using (DataManager dmgr = new DataManager())
        {
            int sum = 0;
            int target = 0;

            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn dc in dt.Columns)
                {
                    if (dc.ColumnName.Contains("ActualA"))
                    {
                        sum += (int) row[dc.ColumnName]; //This adds up all the Actual rows in the grid
                    }
                    if (dc.ColumnName.Contains("TargetA"))
                    {
                        target += (int)row[dc.ColumnName]; //This adds up all the target rows in the grid
                    }

                    if (dc.ColumnName.Contains("ActualB"))
                    {
                        sum += (int) row[dc.ColumnName];
                    }

                    if (dc.ColumnName.Contains("TargetB"))
                    {
                        target += (int) row[dc.ColumnName];
                    }

                    if (dc.ColumnName.Contains("ActualC"))
                    {
                        sum += (int) row[dc.ColumnName];
                    }

                    if (dc.ColumnName.Contains("TargetC"))
                    {
                        target += (int) row[dc.ColumnName];
                    }

                    if (dc.ColumnName.Contains("ActualAA"))
                    {
                        sum += (int) row[dc.ColumnName];
                    }

                    if (dc.ColumnName.Contains("TargetAA"))
                    {
                        target += (int) row[dc.ColumnName];
                    }

                    Color foreColorGreen = Color.Green;
                    Color forecolorRed = Color.Red;
                    if  (sum < target)
                    {
                        sum.Equals(forecolorRed);
                    }
                    else
                    {
                        sum.Equals(foreColorGreen);
                    }

                }
            }
            return sum;
            }
        }

The total label works with just summing the actaul columns i.e

if (dc.ColumnName.Contains("Actual"))
                        {
                            sum += (int) row[dc.ColumnName]; //This adds up all the Actual rows in the grid.

But i have added in the rest and it doesnt change colour and is adding up all the targets so the actual will never show in green even if it worked...

wazz
  • 4,953
  • 5
  • 20
  • 34
  • if you are not doing any manipulation on the data at the user end then wouldn't it be better to get the sum of `target` and `actual` from `DB` instead of doing that on server ? – vikscool Jun 26 '18 at 08:23
  • Or if don't want to do that from db you can use the `Compute()` method in DataTable to get the sum refer to [this](https://stackoverflow.com/a/5893054/2417602) – vikscool Jun 26 '18 at 08:30
  • 1
    What is this supposed to do: `sum.Equals(forecolorRed);`? – wazz Jun 26 '18 at 10:26

0 Answers0