This is a migrated thread and some comments may be shown as answers.

Check Back color of cell

1 Answer 42 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Lee
Top achievements
Rank 1
Lee asked on 20 Sep 2011, 10:11 PM
Okay, I'm successfully changing the backcolor of cells in my grid based on the data being populated.

What I want to do now is when a button is clicked, check to see if any of the cells still have a red background.

However, I cannot seem to get any property that will tell me if the cell has a background color of Color.Red.

Here is the code I've been trying:

for (int iRow = 0; iRow < ProductsRadGridView.Rows.Count; iRow++)
{
          for (int iCell = 2; iCell < 6; iCell++)
          {
                    if (ProductsRadGridView.Rows[iRow].Cells[iCell].Style.BackColor == Color.Red)
                    {

                    }

                    //valid = false;
                    //MessageBox.Show("One or more Line Item Values needs modification. Please make the necessary change.);
                    //return valid;
          }
}

However, the Style.BackColor returns some ambigous name called "ControlDarkDark". This is the value regardless of the back color.

How can I check what the back color really is?

Lee

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 23 Sep 2011, 03:44 PM
Hello Lee,

Thank you for writing.

The approach that you are using will not return any valid results, since the visual cells are being reused, due to the UI virtualization of RadGridView. I will suggest, when you change a cell BackColor to red, also to modify the cell Tag in the CellFormatting event, and then when iterating the grid cells, check the Tag value, to determine if the cell is red or not. Here is an example:
void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
    if (e.RowIndex > -1 && e.ColumnIndex > -1)
    {
        if (e.ColumnIndex == 1 && e.RowIndex % 2 == 0)
        {
            e.CellElement.BackColor = Color.Red;
            e.CellElement.NumberOfColors = 1;
            e.CellElement.DrawFill = true;
            radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag = true;
        }
        else
        {
            radGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Tag = null;
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.NumberOfColorsProperty, Telerik.WinControls.ValueResetFlags.Local);
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local);
        }
    }
}
 
 
private void radButton1_Click(object sender, EventArgs e)
{
    int redCells = 0;
    for (int iRow = 0; iRow < radGridView1.Rows.Count; iRow++)
    {
        for (int iCell = 0; iCell < 3; iCell++)
        {
            if (Convert.ToBoolean(radGridView1.Rows[iRow].Cells[iCell].Tag))
            {
                redCells++;
            }
        }
    }
    MessageBox.Show(redCells + " cells are red");
}

I hope that the provided information addresses your question. Should you have any other questions, do not hesitate to contact us.
 
Best wishes,
Stefan
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Lee
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or