Custom DataGrid Cell Roll Over Example

Posted on March 9, 2009 by Tony Fendall.
Categories: Adobe, Components, Flex.

A few days ago I was asked this question via a previous blog post:

Hello,

Came to this blog searching for a specific issue, which might be nothing for you guys, but have been hitting head for almost two days now.

I have a DataGrid of numbers. All I want to do is that when a mouse rolls over a specific cell, that cell gets highlighted with say, green color, not the whole row.

I have not gotten past this, but as a next step would like to increase the font of the text in that cell as well.. so to emphasize it.

I will appreciate any help or pointers in the right direction.

~Lead

I responded with a brief explanation at the time, but this afternoon I decided to see if I could whip up an example application showing how this could be done.  I managed to come up with some code that is both quite simple and reusable. You can check out my demo application here.

 Cell RollOver Example
Right click on the application to View Source

My demo only shows how to change the background color of the rolled over cell, but once you have that working, changing the text size of the cell should be trivial.  Some of the things too look out for when implementing a feature like this are:

  • Be sure to set userRollOver and selectable to false on your DataGrid. This stops the default rollover behaviour from interfering with your custom rollover.
  • Using the listData property rather than the data property to populate your item renderer is an easy way to make your components more reusable.
  • The DataGrid will always set the backgroundAlpha style of any item renderer to 0, and you cannot change that.  To show a custom background color you must create your background above the root element of your component.

If you have any questions just leave a comment here and I’ll do my best to help you out.

huye
Comment on March 27th, 2009.

Thank you for your good job and share

jay
Comment on May 5th, 2009.

cool

Comment on May 22nd, 2009.

Great job. What would the world be like without those who give their free time? Thanks for the example!

Ji
Comment on August 21st, 2009.

Hi, I have a flex data grid. It contains two column Maths Mark and English Mark. If Math Mark >English Mark for any row then it will set that particular row color as Green.Can you pls suggest me how to do that?