Creating multi-line data grid rows with variable row heights

by Peter deHaan on July 23, 2007

in DataGrid, DataGridColumn

One of my favorite Flex components is the the trusty DataGrid. It seems whenever I build some sort of application or sample, I need a DataGrid to show all sorts of debug data or what have you. Here is a simple little example of a DataGrid with multi-line content and a variable row height.

Full code after the jump.

The following example demonstrates how to set up a DataGrid control to word wrap certain data grid columns, and allow each row to have a variable row height, depending on the amount of text in each row:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/07/23/creating-multi-line-data-grid-rows-with-variable-row-heights/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="white" layout="vertical">

    <mx:Array id="arr">
        <mx:Object articleName="Finding out a characters Unicode character code" data="15" />
        <mx:Object articleName="Setting an icon in an Alert control" data="14" />
        <mx:Object articleName="Setting an icon in a Button control" data="13" />
        <mx:Object articleName="Installing the latest nightly Flex 3 SDK build into Flex Builder 3" data="10" />
        <mx:Object articleName="Detecting which button a user pressed to dismiss an Alert dialog" data="9" />
        <mx:Object articleName="Using the Alert control" data="8" />
        <mx:Object articleName="Formatting data tips in a Slider" data="7" />
        <mx:Object articleName="Downloading the latest Adobe Labs version of Flex 3 SDK/Flex Builder 3 (codename: Moxie)" data="6" />
    </mx:Array>
    <mx:ArrayCollection id="arrColl" source="{arr}" />

    <mx:DataGrid id="dataGrid" dataProvider="{arrColl}" variableRowHeight="true" width="100%" height="100%">
        <mx:columns>
            <mx:DataGridColumn id="articleName" dataField="articleName" headerText="Name of the article in question" wordWrap="true" />
            <mx:DataGridColumn id="data" dataField="data" headerText="ID of the article" />
        </mx:columns>
    </mx:DataGrid>

</mx:Application>

View source is enabled in the following example.

{ 15 comments… read them below or add one }

1 Simon September 7, 2007 at 7:16 am

Is this possible in Flash ?

Reply

2 peterd September 7, 2007 at 7:35 am

Simon,

Unfortunately I don’t think the Flash ActionScript 2.0 or ActionScript 3.0 components allow for variable row height by default. You may be able to create custom renderers that allow this, or there may be 3rd party components that support this, but I’ve never investigated it. Or perhaps

Peter

Reply

3 Fred September 29, 2007 at 4:41 am

Hi,

Unfortunaly it seem that if you are in edit mode (ie datagrid.editable = true)
and you clic into cell then it is put all in a single line.

Do you know how to edit like textarea?

Thanks

Fred

Reply

4 peterd September 30, 2007 at 10:15 pm

Fred,

Not sure if you’re still reading, but I posted a/the solution as a new entry. You can check it out at: “Using a Flex TextArea control as a drop-in item editor”.

Hope that helps,
Peter

Reply

5 pmolaro December 18, 2007 at 11:31 am

Can I take a similar action by plugging in some sort of break tag (like an HTML )? I want to control where my text wraps to the next line.
___________________________________
p m o l a r o

Reply

6 Harry Saputra February 7, 2008 at 6:55 pm

Quote :
Can I take a similar action by plugging in some sort of break tag (like an HTML )? I want to control where my text wraps to the next line.

Yup, i need this too

Reply

7 peterd February 10, 2008 at 12:11 am

pmolaro/Harry Saputra,

You could probably make a custom item renderer which renders HTML text and then use the
tag.

I’ve never tried it, but it should probably work.

Peter

Reply

8 Brixel July 25, 2008 at 6:10 am

Hey guys,

I ran into this post searching for an answer to a problem using this technique. If you have this exact setup, using variableRowHeight=”true” and a column that “wraps”, and put only one row in the data grid, which exceeds the height of the datagrid, the scroll bar will not appear, and trying to force the scroll bar to appear basing it off the measureHeightOfItems and the height of grid can produce a scroll bar, but it’s greyed out.

I’ve seen there are a few bugs related to variableRowHeight, but hadn’t seen a solution to my issue.

To recreate just setup your grid say with a height of 200 pixels and set variableRowHeight=”true” and a column with wordWrap=”true”. Then push one row of data to it that has enough data to force the row height beyond 200 pixels or use carriage returns in the data, etc and the grid will not render the scrollbar.

Peter can you confirm this to be a bug, is there a work around?

Reply

9 Brixel July 25, 2008 at 6:25 am

One additional note.

You can sort of work around this by adding a blank row to the grid, so it thinks there are at least 2 rows and the scrollbar will render, however, even when you do get a scroll bar, if the text block is large enough, it still doesn’t scroll properly to let you see everything, it just jumps to the blank row instead and you miss the rest of the data in the larger row.

Reply

10 Tony September 1, 2008 at 8:38 am

Nice post. I have been messing around with datagrids for a while now and one thing I cannot understand is how flex insists that your rows are padded. I want rows to be only high enough so that the text is not cut off so that one row’s text appears to be on top of the following row’s text. Does anyone know how to do this? Changing the rowheight property does not work, and turning horizontal grid lines off does not either.

Reply

11 GT September 22, 2008 at 9:26 am

Hi, all.

Tony – Ihave the same problem. Also, I don’t understand how it does rowCount – if I try to useuse {dp.length} where dp is the dataProvider, half the time it’s OK, half the time I get those blasted scrollbars. Sometimes I use dp.length+1 or +2 (thinking perhaps the headers are being included) and sometimes that works, but it’s not reliable.

rowCount is the only mechanism I’ve been able to think of to display the entire datagrid without scrollbars (trying to do it via height is a crapshoot). Ideally I would like the entire Application to render itself outwards so that the height of every component is the sum of the heights of its children.

Sorry… off on a tangent.

Cheers

GT
France

Reply

12 Alex C October 21, 2008 at 1:36 pm

Can this method also be used when you load an image in an itemRenderer of a column and thus need to force the selected row to be resized to the image’s new height? Thanks.

Reply

13 Strada March 23, 2009 at 12:21 pm

Did anyone find a workaround to the behaviour mentioned by Brixel in his additional note? I have this problem of not being able to read all text in high rows because of the scrollbar jumping to the next rows beginning instead of scrolling smoothly over the rows.

Reply

14 Strada March 23, 2009 at 1:08 pm

additional note:
mentioned also in bug-reports #SDK-14990 and #SDK-16004 on JIRA (https://bugs.adobe.com/jira)

Reply

15 Carlos August 10, 2009 at 3:29 pm

great,

you solve my problem

Muchas Gracias !!

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: