5 Replies Latest reply on Feb 27, 2009 7:28 AM by christophsk

    Change datagrid row colors using resultdata

    Tor...
      Hi all, I'm trying to modify the row color of a datagrid (Flex 3). The default color is white-grey-white-gray etc. I want to change this color for some rows (depending on the data in the datagrid). For example if a row in my datagrid contains a field with value "TEST" or of a field is null I want the row color to be red.

      In design mode it is possible to change this color for all even or odd rows but this is not what I want. I tried to use the setStyle functions but I can't seem to find the correct properties. I thought this would be an easy task but I can't find the ActionScript code to do this. Does anyone have an idea or a sollution?

      Thanks in advance.
        • 2. Re: Change datagrid row colors using resultdata
          Tor... Level 1
          Thanks atta707 for the quick reaction this (partly) what I need. Now I can change colors or rows when the datagrid is being build. Is it possible to also change rows at runtime? For example I like to change the color on an onclick event...
          • 3. Re: Change datagrid row colors using resultdata
            christophsk
            The most flexible way is to use an item renderer for a datagrid column. Sounds like
            a lot of trouble, but it's not that bad:

            <mx:AdvancedDataGridColumn headerText="My header" itemRenderer="itemRenderers.MyItem Renderer"
            sortable="false"/>

            As the rows are being built, the item renderer
            will be called for each row, so you can perform whatever tests you like. You need
            to perform something like the following in the item renderer (in my case
            'value' is a chunk of XML, so I check the children).

            override public function set data(value:Object):void
            {
            if (value == null || value.children == null)
            {
            return;
            }
            .
            .
            }

            Now whenever the dataprovider changes, the renderer will be called
            and you'll have the most recent view of the data (assuming the
            dataprovider is something like ArrayCollection). Always check if
            value == null - it does happen for reasons that are not clear to me.

            Hope this helps.

            • 4. Re: Change datagrid row colors using resultdata
              Level 7

              "christophsk" <webforumsuser@macromedia.com> wrote in message
              news:go8pbe$gq4$1@forums.macromedia.com...
              > The most flexible way is to use an item renderer for a datagrid column.

              No, the most flexible way is to use a DataGrid with a styleFunction...then
              you can present the information in multiple ways by simply providing
              different styles at runtime. You're already using AdvancedDatagrid, unlike
              the original poster, which has the capability to use a style function built
              in. For those using a DataGrid, you can use this:

              http://flexdiary.blogspot.com/2008/09/extended-datagrid-with-stylefunction.html

              > Sounds
              > like
              > a lot of trouble, but it's not that bad:
              >
              > <mx:AdvancedDataGridColumn headerText="My header"
              > itemRenderer="itemRenderers.MyItem Renderer"
              > sortable="false"/>
              >
              > As the rows are being built, the item renderer
              > will be called for each row, so you can perform whatever tests you like.
              > You
              > need
              > to perform something like the following in the item renderer (in my case
              > 'value' is a chunk of XML, so I check the children).
              >
              > override public function set data(value:Object):void
              > {
              > if (value == null || value.children == null)
              > {
              > return;
              > }
              > .
              > .
              > }
              >
              > Now whenever the dataprovider changes, the renderer will be called
              > and you'll have the most recent view of the data (assuming the
              > dataprovider is something like ArrayCollection). Always check if
              > value == null - it does happen for reasons that are not clear to me.

              When the renderer is first created, there's a moment before the List gives
              it any data.

              Note that if you're going to do any styling, you should do it in
              updateDisplayList, not set data(). That way you can be sure that everything
              that you're trying to style exists. For some other examples of renderers,
              check out this example file:
              http://flexdiary.blogspot.com/2008/09/groupingcollection-example-featuring.html.


              • 5. Re: Change datagrid row colors using resultdata
                christophsk Level 1
                Yes of course, my superlatives get the better of me from time to time.

                Thanks for that clarification. It helps me as well. FYI, your second
                link is broken for me.