Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have telerik radgrid that has an SQL backend. What this function is doing is conditionally formatting the cells with in the radgrid. It checks each column to see if the min and max values are within range and the min and max values change depending on the value in the sample_hour column. My question, is there any more refactoring I can do to this function to make it cleaner and I was having an issue with when the float.parse hit a null value it would break so I put a tryparse and it works now but I'm not sure if that is the right thing to do.

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        //Is it a GridDataItem
        if (e.Item is GridDataItem)
        {
            //Get the instance of the right type
            GridDataItem dataBoundItem = e.Item as GridDataItem;

            //Check the formatting condition
            if (dataBoundItem["sample_hour"].Text == "4hr YP")
            {
                SetFormatting(dataBoundItem["ph"], 4.75f, 5.72f);
                SetFormatting(dataBoundItem["brix"], 17.35f, 22.36f);
                SetFormatting(dataBoundItem["temp"], 89.08f, 90.97f);
                SetFormatting(dataBoundItem["bud"], 3.12f, 40.76f);
                SetFormatting(dataBoundItem["cell_count"], 41.24f, 177.70f);
                SetFormatting(dataBoundItem["Viability"], 69.18f, 103.08f);
                SetFormatting(dataBoundItem["dp4"], 2.55f, 10.89f);
                SetFormatting(dataBoundItem["dp3"], 1.41f, 6.05f);
                SetFormatting(dataBoundItem["maltose"], 0.41f, 8.28f);
                SetFormatting(dataBoundItem["glucose"], -0.26f, 6.69f);
                SetFormatting(dataBoundItem["lactic_acid"], 0.01f, 0.12f);
                SetFormatting(dataBoundItem["glycerol"], 0.33f, 0.57f);
                SetFormatting(dataBoundItem["acetic_acid"], 0.07f, 0.17f);
                SetFormatting(dataBoundItem["ethanol"], .005f, 1.15f);
            }
            if (dataBoundItem["sample_hour"].Text == "YP Drop")
            {
                SetFormatting(dataBoundItem["ph"], 4.39f, 5.45f);
                SetFormatting(dataBoundItem["brix"], 13.75f, 20.74f);
                SetFormatting(dataBoundItem["temp"], 94.07f, 85.93f);
                SetFormatting(dataBoundItem["bud"], 5.37f, 42.55f);
                SetFormatting(dataBoundItem["cell_count"], 79.91f, 448.77f);
                SetFormatting(dataBoundItem["viability"], 83.26f, 102.28f);
                SetFormatting(dataBoundItem["dp4"],2.99f, 7.77f);
                SetFormatting(dataBoundItem["dp3"], -0.32f, 4.72f);
                SetFormatting(dataBoundItem["maltose"], 2.20f, 9.60f);
                SetFormatting(dataBoundItem["glucose"], -2.74f, 5.93f);
                SetFormatting(dataBoundItem["lactic_acid"], -0.02f, 0.22f);
                SetFormatting(dataBoundItem["glycerol"], 0.33f, 0.57f);
                SetFormatting(dataBoundItem["acetic_acid"], -0.01f, 0.08f);
                SetFormatting(dataBoundItem["ethanol"], 1.15f, 3.71f);
            }

            if (dataBoundItem["sample_hour"].Text == "Cook Water")
            {
                SetFormatting(dataBoundItem["ph"], 3.03f, 8.91f);
                SetFormatting(dataBoundItem["brix"], -0.77f, 1.96f);
                SetFormatting(dataBoundItem["dp4"], -0.18f, 0.20f);
                SetFormatting(dataBoundItem["dp3"], -0.021f, 0.02f);
                SetFormatting(dataBoundItem["maltose"], -0.14f, 0.15f);
                SetFormatting(dataBoundItem["glucose"], -0.14f, 0.018f);
                SetFormatting(dataBoundItem["lactic_acid"], -0.015f, 0.02f);
                SetFormatting(dataBoundItem["glycerol"], -0.02f, 0.02f);
                SetFormatting(dataBoundItem["acetic_acid"], 0.001f, 0.086f);
                SetFormatting(dataBoundItem["ethanol"], 0.52f, 1.71f);
            }

            if (dataBoundItem["sample_hour"].Text == "Thin Stillage")
            {
                SetFormatting(dataBoundItem["ph"], 4.45f, 5.39f);
                SetFormatting(dataBoundItem["brix"], 3.31f, 7.38f);
                SetFormatting(dataBoundItem["dp4"], -0.09f, 1.01f);
                SetFormatting(dataBoundItem["dp3"], 0.10f, 0.23f);
                SetFormatting(dataBoundItem["maltose"], 0.33f, 0.59f);
                SetFormatting(dataBoundItem["glucose"], -0.14f, 0.41f);
                SetFormatting(dataBoundItem["lactic_acid"], 0.02f, 0.38f);
                SetFormatting(dataBoundItem["glycerol"], 1.39f, 1.71f);
                SetFormatting(dataBoundItem["acetic_acid"], 0.06f, 0.21f);
                SetFormatting(dataBoundItem["ethanol"], -0.03f, 0.06f);
            }

            if (dataBoundItem["sample_hour"].Text == "5 Hour")
            {
                SetFormatting(dataBoundItem["ph"], 4.68f, 5.66f);
                SetFormatting(dataBoundItem["brix"], 26.60f, 30.63f);
                SetFormatting(dataBoundItem["temp"], 89.85f, 94.41f);
                SetFormatting(dataBoundItem["dp4"], 4.17f, 14.65f);
                SetFormatting(dataBoundItem["dp3"], 1.38f, 3.85f);
                SetFormatting(dataBoundItem["maltose"], 5.35f, 8.91f);
                SetFormatting(dataBoundItem["glucose"], 6.16f, 14.03f);
                SetFormatting(dataBoundItem["lactic_acid"], -0.008f, 0.21f);
                SetFormatting(dataBoundItem["glycerol"], 0.47f, 0.81f);
                SetFormatting(dataBoundItem["acetic_acid"], 0.007f, 0.15f);
                SetFormatting(dataBoundItem["ethanol"], -0.25f, 1.68f);
            }

            if (dataBoundItem["sample_hour"].Text == "10 Hour")
            {
                SetFormatting(dataBoundItem["ph"], 4.49f, 5.41f);
                SetFormatting(dataBoundItem["brix"], 24.51f, 29.82f);
                SetFormatting(dataBoundItem["temp"], 90.39f, 94.39f);
                SetFormatting(dataBoundItem["bud"], 6.02f, 40.56f);
                SetFormatting(dataBoundItem["cell_count"], 75.22f, 264.35f);
                SetFormatting(dataBoundItem["viability"], 78.56f, 101.31f);
                SetFormatting(dataBoundItem["dp4"], 6.176f, 10.51f);
                SetFormatting(dataBoundItem["dp3"], 0.78f, 3.03f);
                SetFormatting(dataBoundItem["maltose"], 6.12f, 9.22f);
                SetFormatting(dataBoundItem["glucose"], 6.88f, 12.02f);
                SetFormatting(dataBoundItem["lactic_acid"], 0.008f, 0.29f);
                SetFormatting(dataBoundItem["glycerol"], 0.59f, 0.94f);
                SetFormatting(dataBoundItem["acetic_acid"], 0.016f, 0.174f);
                SetFormatting(dataBoundItem["ethanol"], 0.09f, 2.90f);
            }

            if (dataBoundItem["sample_hour"].Text == "20 Hour")
            {
                SetFormatting(dataBoundItem["ph"], 4.25f, 5.12f);
                SetFormatting(dataBoundItem["brix"], 17.58f, 22.53f);
                SetFormatting(dataBoundItem["temp"], 89.43f, 94.60f);
                SetFormatting(dataBoundItem["bud"],-1.98f, 29.56f);
                SetFormatting(dataBoundItem["cell_count"], 116.58f, 352.18f);
                SetFormatting(dataBoundItem["viability"], 83.59f, 102.07f);
                SetFormatting(dataBoundItem["dp4"], 3.81f, 6.42f);
                SetFormatting(dataBoundItem["dp3"], 0.28f, 0.59f);
                SetFormatting(dataBoundItem["maltose"], 2.38f, 6.62f);
                SetFormatting(dataBoundItem["glucose"], 2.14f, 6.88f);
                SetFormatting(dataBoundItem["lactic_acid"], -0.10f, 0.46f);
                SetFormatting(dataBoundItem["glycerol"], 1.03f, 1.41f);
                SetFormatting(dataBoundItem["acetic_acid"], 0.014f, 0.146f);
                SetFormatting(dataBoundItem["ethanol"], 6.15f, 8.38f);
            }

            if (dataBoundItem["sample_hour"].Text == "30 Hour")
            {
                SetFormatting(dataBoundItem["ph"], 4.19f, 5.09f);
                SetFormatting(dataBoundItem["brix"], 13.31f, 18.80f);
                SetFormatting(dataBoundItem["temp"], 87.06f, 92.72f);
                SetFormatting(dataBoundItem["dp4"], 1.41f, 4.12f);
                SetFormatting(dataBoundItem["dp3"], 0.29f, 0.63f);
                SetFormatting(dataBoundItem["maltose"], -0.22f, 1.53f);
                SetFormatting(dataBoundItem["glucose"], 2.08f, 6.27f);
                SetFormatting(dataBoundItem["lactic_acid"], -0.00166f, 0.369f);
                SetFormatting(dataBoundItem["glycerol"], 1.15f, 1.54f);
                SetFormatting(dataBoundItem["acetic_acid"], 0.031f, 0.149f);
                SetFormatting(dataBoundItem["ethanol"], 9.03f, 11.55f);
            }

            if (dataBoundItem["sample_hour"].Text == "40 Hour")
            {
                SetFormatting(dataBoundItem["ph"], 4.23f, 5.15f);
                SetFormatting(dataBoundItem["brix"], 10.54f, 16.28f);
                SetFormatting(dataBoundItem["temp"], 84.98f, 92.20f);
                SetFormatting(dataBoundItem["dp4"], 0.61f, 1.88f);
                SetFormatting(dataBoundItem["dp3"], 0.17f, 0.47f);
                SetFormatting(dataBoundItem["maltose"], 0.24f, 0.52f);
                SetFormatting(dataBoundItem["glucose"], 0.06f, 4.87f);
                SetFormatting(dataBoundItem["lactic_acid"], 0.0019f, 0.37f);
                SetFormatting(dataBoundItem["glycerol"], 1.24f, 1.66f);
                SetFormatting(dataBoundItem["acetic_acid"], 0.04f, 0.16f);
                SetFormatting(dataBoundItem["ethanol"], 11.31f, 13.89f);
            }

            if (dataBoundItem["sample_hour"].Text == "50 Hour")
            {
                SetFormatting(dataBoundItem["ph"], 4.28f, 5.38f);
                SetFormatting(dataBoundItem["brix"], 9.47f, 14.14f);
                SetFormatting(dataBoundItem["temp"], 85.07f, 91.87f);
                SetFormatting(dataBoundItem["dp4"], -0.013f, 0.97f);
                SetFormatting(dataBoundItem["dp3"], 0.1001f, 0.28f);
                SetFormatting(dataBoundItem["maltose"], 0.2207f, 0.57f);
                SetFormatting(dataBoundItem["glucose"], -1.19f, 2.23f);
                SetFormatting(dataBoundItem["lactic_acid"], 0.0027f, 0.37f);
                SetFormatting(dataBoundItem["glycerol"], 1.22f, 1.76f);
                SetFormatting(dataBoundItem["acetic_acid"], -0.060f, 0.27f);
                SetFormatting(dataBoundItem["ethanol"], 12.88f, 14.94f);
            }

            if (dataBoundItem["sample_hour"].Text == "Drop")
            {
                SetFormatting(dataBoundItem["ph"], 4.44f, 5.46f);
                SetFormatting(dataBoundItem["brix"], 10.01f, 13.48f);
                SetFormatting(dataBoundItem["temp"], 84.28f, 92.83f);                 
                SetFormatting(dataBoundItem["flevel"], 88.10f, 94.81f);
                SetFormatting(dataBoundItem["dp4"], 0.086f, 0.46f);
                SetFormatting(dataBoundItem["dp3"], 0.087f, 0.17f);
                SetFormatting(dataBoundItem["maltose"], 0.23f, 0.59f);
                SetFormatting(dataBoundItem["glucose"], -0.53f, 0.74f);
                SetFormatting(dataBoundItem["lactic_acid"], 0.0039f, 0.36f);
                SetFormatting(dataBoundItem["glycerol"], 1.29f, 1.74f);
                SetFormatting(dataBoundItem["acetic_acid"], 0.032f, 0.181f);
                SetFormatting(dataBoundItem["ethanol"], 13.33f, 15.08f);
            }

            if (dataBoundItem["sample_hour"].Text == "Beer Well")
            {
                SetFormatting(dataBoundItem["ph"], 5.69f, 4.60f);
                SetFormatting(dataBoundItem["brix"], 9.62f, 13.54f);
                SetFormatting(dataBoundItem["temp"], 84.93f, 94.92f);        
                SetFormatting(dataBoundItem["dp4"], -0.01f, 0.59f);
                SetFormatting(dataBoundItem["dp3"], 0.08f, 0.14f);
                SetFormatting(dataBoundItem["maltose"], 0.2008f, 0.58f);
                SetFormatting(dataBoundItem["glucose"], -0.31f, 0.576f);
                SetFormatting(dataBoundItem["lactic_acid"], 0.016f, 0.354f);
                SetFormatting(dataBoundItem["glycerol"], 1.27f, 1.69f);
                SetFormatting(dataBoundItem["acetic_acid"], 0.0111f,0.2085f);
                SetFormatting(dataBoundItem["ethanol"], 12.98f, 15.15f);
            }
        }
    }

    private void SetFormatting(TableCell cell, float minValue, float maxValue)
    {

            float value = 0;
            float.TryParse(cell.Text, out value);

            if (value < minValue || value > maxValue)
            {
                cell.BackColor = Color.Yellow;
                cell.ForeColor = Color.Black;
                cell.Font.Bold = true;
            }


    }
share|improve this question

2 Answers 2

I think it would be a good idea to replace all those ifs with a mapping dictionaries like this:

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    // define all possible ranges
    var ranges = new Dictionary<string, Range>()
    {
        {"ph", new Range(5.72f, 4.75f) },
        {"brix", new Range(17.35f, 22.36f) }
    };

    // define which ranges belong to which cell (Text)
    var cellRanges = new Dictionary<string, string[]>()
    {
        { "4hr YP", new [] { "ph", "brix" } },
        { "YP Drop", new [] { "ph", "brix" } }
    };

    // an anonymous method setting the formattings
    var applyFormatting = new Action<DataBoundItemType, string>((dataBoundItem, sampleHour) =>
    {
        var currentCellRanges = cellRanges[sampleHour];
        foreach (var rangeName in currentCellRanges)
        {
            var range = ranges[rangeName];
            SetFormatting(dataBoundItem[rangeName], range.Min, range.Max);
        }
    });

    //Is it a GridDataItem
    if (e.Item is GridDataItem)
    {
        //Get the instance of the right type
        GridDataItem dataBoundItem = e.Item as GridDataItem;

        // call the anonymous method to upate the formatting for current Text
        applyFormatting(dataBoundItem, dataBoundItem["sample_hour"].Text);
    }
}

You'll also need a class that holds the ranges:

private class Range
{
    public Range(float min, float max)
    {
        Min = min;
        Max = max;
    }
    public float Min;
    public float Max;
}

This way you would be able to update anything at once without searching for all occurances of a value.

As far as the TryParse is concered. You're doing it right. That's exacly the purpose of this method. You should however reconsider this decision as @Rick Davin already mentioned.

share|improve this answer
    
This would work but the min and max range is different for each cellrange. Sorry I know the original post showed that but they are all different. I have updated the code in the post. How do you store the values in the class. @t3chb0t –  llerdal Aug 13 at 20:59
    
The same ranges (e.g. px, lactic_acid...) are always the same, in each case, so why repeat yourself? Create dictionaries for them. You need two of them. In the first one, you define all possible ranges in the second one you define which ranges apply to which sample_hour. –  t3chb0t Aug 13 at 21:02
    
I see you've updated your code... now it won't work anymore :-) –  t3chb0t Aug 13 at 21:03
    
Sorry I didnt think you would look at the data so close...yes all the data points are different. –  llerdal Aug 13 at 21:04
    
do you have any input on the new code? –  llerdal Aug 15 at 4:19

SetFormatting Method

Switching to TryParse helps suppress the exception but what do you want to do if cell.Text was null? Do you want to color the cell or not? The logic you currently have would color the cell upon a null value with minValue > 0. Is that what you want?

My suggestion would be to use: if (float.TryParse(cell.Text, out value)) or if (!float.TryParse(cell.Text, out value)) accordingly to explicitly deal with how you want with null values rather than have it fall through the cracks and produce undesired effects.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.