0

I have a conditional statement that is bound to my gridview now that will check the sql data that is passed and if it there is a certain phrase then the gridview cell will turn pink. But if the text is not there then it will turn green.

Here is the statement I have so far:

  if (dataItem != null)
            {
                var label = dataItem["Client"].FindControl("ClientLabel") as Label;
                if (label != null)
                {
                    var item = dataItem;
                    var text = label.Text;

                    if (text == "Complete") 
                    {
                        item["ClientServer"].BackColor = Color.Lime;

                    }
                    else if (text != "Complete") 
                    {
                        item["ClientServer"].BackColor = Color.Salmon;
                    }
                }

So this works and will turn the cell green or pink depending on the text, but is there a way to hide the word "complete" and still turn the cell the correct color?

CSharpDev4Evr
  • 535
  • 1
  • 11
  • 35
  • 1
    You want to hide the word "Complete" in your source code? Or you don't want to hard code that value? Try and ask your question in a different way. – Nick DeVore Dec 19 '13 at 20:50
  • @NickDeVore, I just want to hide the word complete. The user will manually enter this in the gridview. – CSharpDev4Evr Dec 19 '13 at 20:57

3 Answers3

0

How about clearing out the text property of the item (setting its sub-item text to null)? And if you eventually need it in the future you could store it in a temporary variable.

Rodrigo Silva
  • 432
  • 1
  • 6
  • 18
0

Did you try this method

Label label = dataItem["Client"].FindControl("ClientLabel") as Label;

if (text == "Complete") 
{
    item["ClientServer"].BackColor = Color.Lime;
    lable.visible = false;
}
Kirk
  • 4,957
  • 2
  • 32
  • 59
0

There is a similar question, that contains good examples on the following link:

Getting value from a cell from a gridview on RowDataBound event

 protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[0].Text.Contains("Complete"))
            {
                e.Row.Cells[1].BackColor = System.Drawing.Color.Lime;
            }
        }
    }

Regards

Community
  • 1
  • 1
Ricardo Huertas
  • 1,296
  • 1
  • 14
  • 15