Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
235
Dynamically change the background color of the column
posted

Hi.

I'm working with a WDG like this:
DayOfWeek                           ColorCode
----------------------+------------------------
Monday                          Green
Tuesday                         Orange
Wednesday                     Blue
...

ColorCode column represent the color of the day, so I want that column to be painted.
We have database tables for DayOfWeek and Colors, and each color has RGB components. You know there is millions of colors, so, I can't write code CssClass for each color. I know how to set style based on CssClass atributte in InitializeRow event:

  e.Row.Items.FindItemByKey("ColorCode").CssClass = "MyCssClassForGreen"

But, as I said, there are millions of colors. I want to know if is there a way to set style more dinamically? For example, get the Red, Green and Blue values, and set background color for that cell. Another option we could use is to colorize the DropDownEditorProvider, is it possible?

I am attaching a small example about this functionality using version 14.2.20142.2146. Let me know if we are coding in the correct way.

Thanks!

ColorizeDropDownEditorProvider.rar
  • 20255
    Suggested Answer
    Online posted

    Hello,

    Thank you for using our forum.

    Yes, your approach to set the styles on InitializeRow server event is correct. About your other question, no it is not possible to colorize the DropDownEditorProvider and this to affect the cell style, our editor providers are visible/available only on cell editing.

    In order to show you how to dynamically create some color and set it to a particular cell/column, I have created a sample for you. Basically my approach is to create custom style page element and to append to it my dynamically created css classes (on page load). Have a look at the sample and let me know if I may be of further assistance.

    Code snippet:

    C#:

    protected void Page_Load(object sender, EventArgs e)
     {
         if (!IsPostBack)
         {
             WebDataGrid1.DataSource = new TestData().GetData();
     
             StringBuilder cssSylesBuilder = new StringBuilder();
             cssSylesBuilder.Append(".BG1 {background-color: red !important;}");
             cssSylesBuilder.Append(".BG2 {background-color: blue !important;}");
             cssSylesBuilder.Append(".BG3 {background-color: green !important;}");
             Page.ClientScript.RegisterClientScriptBlock(
                 this.GetType(), 
                 "MyCss", 
                 "var cssStyles = '" + cssSylesBuilder.ToString() + "'", 
                 true);
         }
     }
     protected void WebDataGrid1_InitializeRow(object sender, Infragistics.Web.UI.GridControls.RowEventArgs e)
     {
     
         if ((int)e.Row.DataKey[0] >= 0 &&
             (int)e.Row.DataKey[0] < 3)
         {
             for (int i = 0; i < e.Row.Items.Count; i++)
             {
                 e.Row.Items[i].CssClass = "BG1";
             }
         }
         else if ((int)e.Row.DataKey[0] >= 3 &&
             (int)e.Row.DataKey[0] < 6)
         {
             for (int i = 0; i < e.Row.Items.Count; i++)
             {
                 e.Row.Items[i].CssClass = "BG2";
             }
         }
         else
         {
             for (int i = 0; i < e.Row.Items.Count; i++)
             {
                 e.Row.Items[i].CssClass = "BG3";
             }
         }
     }

    Markup:

    <script type="text/javascript" id="igClientScript">
        function WebDataGrid1_Grid_Initialize(sender, eventArgs) {
            var styleElement = document.createElement('style');
            var styleClasses = cssStyles;
            styleElement.setAttribute("type""text/css");
     
            var csstxt = $(styleElement).css('cssText');
            $(styleElement).html(styleClasses);
     
            var headElement = document.getElementsByTagName('head')[0];
            headElement.appendChild(styleElement);
        }
    </script>
    CreateDynamicStyleElement(CSS).zip