How To Get DataGrid Column And Row From Coordinate (x,y) In Flex

youre-kidding-right-iconWe have introduced how to set background color for the headers of DataGrid in Flex, sometimes, we also need to get the row and column value in DataGrid, do you know how to do it? The following is a simple tip for this. Hope you can enjoy!

Extend to DataGrid, if use listItems property, you can get an instance of the dialog item. Compare between x, y, width, height and  the location of cell (x, y), you can get the row and column value. Normally the source code similar:

public function checkRendererPoint(p:Point):void {
    var row:int;
    var col:int;
    var rowLength:int;
    var colLength:int;
    var rendererRect:Rectangle;
    var renderer:DataGridItemRenderer;
    rowLength = listItems.length;
    for(row = 0; row < rowLength; row++) {
        colLength = listItems[row].length;
        for(col = 0; col < colLength; col++) {
            renderer = listItems[row][col] as DataGridItemRenderer;
            rendererRect = new Rectangle(renderer.x,
                                         renderer.y + headerHeight,
                                         renderer.x + renderer.width,
                                         renderer.y + renderer.height + headerHeight );
            if( rendererRect.x <= p.x && rendererRect.width >= p.x &&
                rendererRect.y <= p.y && rendererRect.height>= p.y    ) {
                trace("hit : ", renderer, row, col);
            }
        }
    }
}

However, if you scrolled the DataGrid, could you also get row, column?  In fact, only need to count verticalScrollPosition and horizontalScrollPosition, it will work fine!

BTW, do you have try to look at your first program after you writing programs for N or NN years?  I think that will be very interesting for most programmers. :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

Leave a Reply