The following example shows how you can resize a DataGrid control in Flex using the rowCount property. By setting the rowCount property, the DataGrid control is automatically resized so that only the specified number of rows are displayed.

In Flex 3 (post beta 2, you can grab nightly versions of the Flex 3 SDK from the Adobe Labs Flex 3 SDK page) the DataGrid control’s rowCount and lockedRowCount properties do not include the header whereas in earlier versions of Flex 3 and Flex 2 they did. For example, the old behavior was to display 3 rows and the data grid header if you set the row count to 4. The new behavior is to display 4 rows and the data grid header (for a total of 5 rows). Yay!

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/01/resizing-a-flex-datagrid-control-using-the-rowcount-property/ -->
<mx:Application name="DataGrid_rowCount_test_2"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">
 
    <mx:Array id="arr">
        <mx:Object col1="1" col2="A" col3="a" />
        <mx:Object col1="2" col2="B" col3="b" />
        <mx:Object col1="3" col2="C" col3="c" />
        <mx:Object col1="4" col2="D" col3="d" />
        <mx:Object col1="5" col2="E" col3="e" />
        <mx:Object col1="6" col2="F" col3="f" />
    </mx:Array>
 
    <mx:ApplicationControlBar dock="true">
        <mx:Form>
            <mx:FormItem label="rowCount:">
                <mx:HSlider id="slider"
                        minimum="0"
                        maximum="{arr.length+1}"
                        value="4"
                        liveDragging="true"
                        snapInterval="1"
                        tickInterval="1"
                        dataTipPrecision="0" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>
 
    <mx:DataGrid id="dataGrid"
            dataProvider="{arr}"
            rowCount="{slider.value}" />
 
</mx:Application>

View source is enabled in the following example.

For an example of resizing the List control using the rowCount property, see “Resizing a Flex List control using the rowCount property”.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

10 Responses to Resizing a Flex DataGrid control using the rowCount property

  1. peterd says:

    The previous example was compiled using the Flex 3 SDK (Version 3.0 build 186046).

  2. Patrick Kellogg says:

    Do you have any idea why rowCount of zero shows 7 rows? I assume that is a bug. Thanks.

  3. ÐâŖк6 says:

    Got the exactly same error, but instead of zero rows I had five, and when I added another one, it became 11 0_0.

  4. hello -

    is there any way of resizing the stage, that is to say, the dimensions of the embed tag, dynamically?

    Leonhard; berlin

  5. Tim says:

    There seems to be a bug with showHeaders=false.
    When the rowCount is 5 and there are 5 items, it shows 6 rows when it should only show 5.

  6. DavidD says:

    If you have a main datagrid on a page with a percent height to take the maximum vertical place available.
    And you would like to paginate to avoid scrollbars,…
    Is there a way to get the actual rowcount displayed ? goal is to set the page size of my pagination system dynamically, cause it can be dependent of the resolution and browser settings of the user.

    thank you.

  7. asifdegr8 says:

    i want to loop through datagrid rows in flex. how do i know how many rows are there in a data grid?

  8. Ranjith says:

    I want the rows to be increased or decreased when i expand or collapse the tree structure.

    I am using a hierarchical data in advanced grid. i need the row count of the advanced grid to be the same as its collection length (ie., grid.mx_internal::collectionLength).

    rowCount=”{myADG.mx_internal::collectionLength}”

    This is not working. Any other suggestions ??

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree