user1732364
user1732364

Reputation: 993

Telerik Radgridview Winforms Change Cell/Row background color

I am simply wanting to run a validation check fired from a button event on the form. For each row inside the grid, it checks the first cell to see if the value matches a string. For every one that fails, i'm wanting it to either change the cell or the row background color to be RED. When the user corrects the cell value and hit the button again, it will reverse back to the default color. For the life of me i can't get it to change the color. I know there is an event for cellformatting but surely i do not have to go through an event just to alter the style of a cell/row. I am trying the below code without any luck. All the examples i'v seen are event based so hoping i can avoid this entirely.

if (!ValidateParametersExistInScript(rtxtQuery.Text))
{
    GridViewCellInfo cell = rgvNewParameters.Rows[0].Cells["ParameterName"];
    cell.Style.BackColor = Color.Red;
    cell.Style.DrawFill = true;
    MessageBox.Show("Parameters do not match definition inside query, " + 
                    "please check spelling and try again.");
}

Upvotes: 2

Views: 9163

Answers (2)

Taja_100
Taja_100

Reputation: 449

 private void radGridLedgerAccount_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.RowIndex == 0  || e.RowIndex==radGridLedgerAccount.Rows.Count-1)
            {

                e.CellElement.DrawFill = true;
                e.CellElement.ForeColor = Color.White;
                e.CellElement.NumberOfColors = 1;
                e.CellElement.BackColor = Color.Red;
            }
            else
            {
                if (e.CellElement.ColumnInfo.Name == "months") //checking for the text in header cell
                {
                    if (e.RowIndex != 0)
                    {
                        System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#6599FF");
                        e.CellElement.DrawFill = true;
                        e.CellElement.ForeColor = Color.White;
                        e.CellElement.NumberOfColors = 1;
                        e.CellElement.BackColor = col;
                    }
                }

                else
                {
                    e.CellElement.DrawFill = true;
                    e.CellElement.ForeColor = Color.Black;
                    e.CellElement.NumberOfColors = 1;
                    e.CellElement.BackColor = Color.White;
                    e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
                }

            }

Upvotes: 2

Silas Reinagel
Silas Reinagel

Reputation: 4223

You must first set CustomizeFill to true before you can modify the back color of the GridViewCellInfo.

if (!ValidateParametersExistInScript(rtxtQuery.Text))
{
    GridViewCellInfo cell = rgvNewParameters.Rows[0].Cells["ParameterName"];
    cell.Style.CustomizeFill = true;
    cell.Style.BackColor = Color.Red;
    cell.Style.DrawFill = true;
    MessageBox.Show("Parameters do not match definition inside query, " + 
                    "please check spelling and try again.");
}

Upvotes: 6

Related Questions