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

unable to set the backcolor for a GridViewCommandColumn with conditional formatting

1 Answer 50 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Anu
Top achievements
Rank 1
Anu asked on 09 Sep 2011, 10:36 PM

I am trying to set the backcolor of a gridview GridViewCommandColumn using conditional formating, but it is not showing the colors:
I am using gridview version 2011.1.11.419
Please help

Thanks

Here is the code:

 

 

 

 

 

 

 

void radGV_CellFormatting(object sender, CellFormattingEventArgs e)

 

{


if (mycondn == true)
                    {
                        e.CellElement.DrawFill = true;
                       e.CellElement.ForeColor = Color.Blue;
                        e.CellElement.NumberOfColors = 1;
                        e.CellElement.BackColor = System.Drawing.Color.Gold;
                     
                    }
                 else                    {
                        e.CellElement.DrawFill = true;
                        e.CellElement.ForeColor = Color.Blue;
                        e.CellElement.NumberOfColors = 1;
                         e.CellElement.BackColor = System.Drawing.Color.LightBlue;
  
                    }
}

my cell is created like this:

//data base field name is ScDate
 GridViewCommandColumn colSchDelDate = new GridViewCommandColumn("ScDate");
  colSchDelDate.UseDefaultText = false;
  colSchDelDate.Width = 80;
  colSchDelDate.HeaderText = "Sc Date";
  colSchDelDate.ReadOnly = true;
  colSchDelDate.FormatString = "{0:MM/dd/yy}";
  colSchDelDate.TextAlignment = ContentAlignment.MiddleCenter;
  radGV.MasterTemplate.Columns.Add(colSchDelDate);

}



1 Answer, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 12 Sep 2011, 07:52 AM
Hello Anu,

I believe that the problem is not that the grid is not formatting the columns. If you want the button itself to have the same color as the grid column you should format it separately, like the following:

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (myCond)
    {
        e.CellElement.DrawFill = true;
        e.CellElement.ForeColor = Color.Blue;
        e.CellElement.NumberOfColors = 1;
        e.CellElement.BackColor = System.Drawing.Color.Gold;
 
    }
    else
    {
        e.CellElement.DrawFill = true;
        e.CellElement.ForeColor = Color.Blue;
        e.CellElement.NumberOfColors = 1;
        e.CellElement.BackColor = System.Drawing.Color.LightBlue;
 
    }
 
    if (e.CellElement is GridCommandCellElement)
    {
        var commandCell = e.CellElement as GridCommandCellElement;
        commandCell.CommandButton.ButtonFillElement.NumberOfColors = 1;
        commandCell.CommandButton.ButtonFillElement.ForeColor = Color.Blue;
        commandCell.CommandButton.ButtonFillElement.BackColor = myCond
                                                                    ? System.Drawing.Color.Gold
                                                                    : System.Drawing.Color.LightBlue;
    }
}

Best Regards,
Emanuel Varga
Tags
GridView
Asked by
Anu
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Share this question
or