8 Replies Latest reply on May 17, 2009 2:19 AM by thesteelmam

    datagrid problem

    thesteelmam

      hi all

      how r u

      i have a small problem in datagrid and hope any one help me

      i created a datagrid that each row can has its own color i used this code


      package Models
      {
            import flash.display.Sprite;
            
            import mx.collections.ArrayCollection;
            import mx.controls.DataGrid;  
         
           public class myDataGrid extends DataGrid  
           {  
               private var _coloredRowIndex:int;  
               private var _rowColorCode:uint;  
               private var _setColorRowIndex:Boolean;   
         
               public function set setColorRowIndex(b:Boolean):void  
               {  
                   _setColorRowIndex = b;             
               }  
         
               public function get setColorRowIndex():Boolean  
               {  
                   return _setColorRowIndex;  
               }  
         
               public function set coloredRowIndex(i:int):void  
               {  
                   _coloredRowIndex = i;  
               }  
         
               public function get coloredRowIndex():int  
               {  
                   return _coloredRowIndex;  
               }  
         
               public function set rowColorCode(colorValue:uint):void  
               {  
                   _rowColorCode = colorValue;  
               }  
         
               public function get rowColorCode():uint  
               {  
                   return _rowColorCode;  
               }  
         
               override protected function drawRowBackground(s:Sprite,rowIndex:int,y:Number,height:Number,color:uint,dataIndex:int):void  
               {  
                  try{
                            if(setColorRowIndex == true)  
                   {  
                       if(rowIndex == coloredRowIndex)  
                       {  
                           color = rowColorCode;  
                           super.drawRowBackground(s,rowIndex,y,height,color,dataIndex);  
                       }  
                       else  
                           super.drawRowBackground(s,rowIndex,y,height,color,dataIndex);  
                   }  
                   else  
                   {  
                       if((dataProvider as ArrayCollection).getItemAt(rowIndex,0).status==0)  
                       {  
                           color = 0xffffff;  
                           super.drawRowBackground(s,rowIndex,y,height,color,dataIndex);  
                       } 
                       else if((dataProvider as ArrayCollection).getItemAt(rowIndex,0).status==1)  
                       {  
                           color = 0xdf8b41;  
                           super.drawRowBackground(s,rowIndex,y,height,color,dataIndex);  
                       } 
                       else if((dataProvider as ArrayCollection).getItemAt(rowIndex,0).status==2)  
                       {  
                           color = 0xa9a79f;  
                           super.drawRowBackground(s,rowIndex,y,height,color,dataIndex);  
                       } 
                           else if((dataProvider as ArrayCollection).getItemAt(rowIndex,0).status==3)  
                       {  
                           color = 0x00ff00;
                           super.drawRowBackground(s,rowIndex,y,height,color,dataIndex);  
                       }   
                       else  
                           super.drawRowBackground(s,rowIndex,y,height,color,dataIndex);  
                   }  
                  }
                  catch(err:Error){
                       
                  }
            }  
           }  
       } 
      
      

      it works very good but the problem is :

      when i scroll the datagrid vertically the rows don't update its color, i mean the the rows scrolled but the colors don't


      any help?

      thanx alot

        • 1. Re: datagrid problem
          Barna Biro Level 3

          Hi there,

           

          Try to invalidate the display or the properties of the dataGrid. You need to do this to force revalidation so that it will update it's properties whenever you scroll it. Read up on invalidateProperties and the other methods that can help you out with this.

           

          With best regards,

          Barna Biro

          1 person found this helpful
          • 2. Re: datagrid problem
            thesteelmam Level 1

            i tried this but not success

            any help?

            • 3. Re: datagrid problem
              Barna Biro Level 3

              Hi again,

               

              I'll try to take a closer look at it a bit later. Until then, don't give up.

               

              With best regards,

              Barna Biro

              Blog: http://blog.wisebisoft.com

              • 4. Re: datagrid problem
                Barna Biro Level 3

                Ok, I'm back. Try this out:

                 

                package
                {
                    import flash.display.Graphics;
                    import flash.display.Shape;
                    import flash.display.Sprite;
                    
                    import mx.controls.DataGrid;
                    import mx.core.FlexShape;
                
                    public class CustomDG extends DataGrid
                    {
                        private var _strongColor:uint = 0x9BCE49;
                        private var _fadedColor:uint = 0xC9FDC8;
                        
                        override protected function drawRowBackground(s:Sprite,
                                                                      rowIndex:int,
                                                                      y:Number,
                                                                      height:Number,
                                                                      color:uint,
                                                                      dataIndex:int):void  
                        {
                            var background:Shape;
                            if (rowIndex < s.numChildren)
                            {
                                background = Shape(s.getChildAt(rowIndex));
                            }
                            else
                            {
                                background = new FlexShape();
                                background.name = "background";
                                s.addChild(background);
                            }
                            
                            background.y = y;
                            color == 0xFFFFFF ? color = _fadedColor : color = _strongColor;
                            
                            var graphics:Graphics = background.graphics;
                            graphics.clear();
                            graphics.beginFill(color);
                            graphics.drawRect(0, 0, width, height);
                            graphics.endFill(); 
                        }
                    }
                }
                

                 

                I don't really have time to play around with the alpha part and make it work with a single color that is once faded and once not. I chose to play around with 2 different colors ( a strong version and a light version ). Obviously, you can play around with the alpha too, you just need to understand the mechanism behind the updating.

                 

                With best regards,

                Barna Biro

                Blog: http://blog.wisebisoft.com

                 

                EDIT: I think that it's even better to use 2 different colors because this way, you have even more control over the appearance. You are not limited this way to a single color and it's faded version. You could combine 2 totally different colors ( like blue and red ).

                • 5. Re: datagrid problem
                  thesteelmam Level 1

                  thank you very much

                  i will test the code soon and i will tell u the result

                  thanx

                  • 6. Re: datagrid problem
                    thesteelmam Level 1

                    hi

                    i tested the code but i faced the same problem

                    while scrolling the colored rows can't update its colors

                    this is the changes i made in your code

                    package Models{
                         
                         import flash.display.Graphics;
                         import flash.display.Shape;
                         import flash.display.Sprite;
                         
                         import mx.collections.ArrayCollection;
                         import mx.controls.Alert;
                         import mx.controls.DataGrid;
                         import mx.core.FlexShape;
                     
                        public class cDataGrid extends DataGrid{
                            private var _strongColor:uint = 0x9BCE49;
                            private var _fadedColor:uint = 0xC9FDC8;
                            
                            override protected function drawRowBackground(s:Sprite,rowIndex:int,y:Number,height:Number,color:uint,dataIndex:int):void {
                                var background:Shape;
                                if (rowIndex < s.numChildren){
                                    background = Shape(s.getChildAt(rowIndex));
                                }
                                else{
                                    background = new FlexShape();
                                    background.name = "background";
                                    s.addChild(background);
                                }
                                background.y = y;
                                try{
                                     if((dataProvider as ArrayCollection).getItemAt(rowIndex,0).status==0)  
                                     {  
                                         color = 0xffffff;  
                                     } 
                                     else if((dataProvider as ArrayCollection).getItemAt(rowIndex,0).status==1)  
                                     {  
                                         color = 0xdf8b41;  
                                     } 
                                     else if((dataProvider as ArrayCollection).getItemAt(rowIndex,0).status==2)  
                                     {  
                                         color = 0xa9a79f;  
                                     } 
                                         else if((dataProvider as ArrayCollection).getItemAt(rowIndex,0).status==3)  
                                     {  
                                         color = 0x00ff00;
                                     } 
                                     
                                }
                                catch(err:Error){
                                     
                                }
                                var graphics:Graphics = background.graphics;
                                graphics.clear();
                                graphics.beginFill(color);
                                graphics.drawRect(0, 0, width, height);
                                graphics.endFill(); 
                            }
                        }
                    }
                    

                    i used all invalidate methods but got the same result !!

                    any help doing this ?

                    thanx alot

                    • 7. Re: datagrid problem
                      Barna Biro Level 3

                      Did you even try the code I posted? I'm quite sure you didn't. When you scroll, the rows get updated correctly ( just like with the default DataGrid component ). The problem is that you are messing the whole thing up by trying to color the row depending on status. You should first read up on the DataGrid component and understand how it functions, you won't advance by copy-pasting code that you don't understand.

                       

                      Once you read up on the component and understand how it's built to function then you'll see the problem in your code too.

                       

                      With best regards,

                      Barna Biro

                      Blog: http://blog.wisebisoft.com

                      • 8. Re: datagrid problem
                        thesteelmam Level 1

                        thank you too much

                        i will do then come back