unable to set the backcolor for a GridViewCommandColumn with conditional formatting

2 posts, 1 answers
  1. Anu
    Anu avatar
    8 posts
    Member since:
    May 2011

    Posted 09 Sep 2011 Link to this post

    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);

    }



  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 12 Sep 2011 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top