OK, this is a pretty neat trick. Flex makes it pretty easy to customize the look and feel of the DataGrid. For example, the following code lets you toggle the data grid’s horizontalGridLines and verticalGridLines styles on or off, change the alternating row background colors so each row is a plain white, and sets the horizontalGridLineColor and verticalGridLineColor styles to red.

Full code after the jump.

The following example sets the horizontal and vertical grid lines to red, disables the alternatingItemColors style by setting it to white, and controls the horizontalGridLines and verticalGridLines styles by using two CheckBox controls:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/07/23/toggling-a-datagrid-controls-horizontal-grid-lines-and-vertical-grid-lines/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object label="item 1" data="1" />
        <mx:Object label="item 2" data="2" />
        <mx:Object label="item 3" data="3" />
        <mx:Object label="item 4" data="4" />
        <mx:Object label="item 5" data="5" />
        <mx:Object label="item 6" data="6" />
        <mx:Object label="item 7" data="7" />
        <mx:Object label="item 8" data="8" />
        <mx:Object label="item 9" data="9" />
        <mx:Object label="item 0" data="0" />
    </mx:Array>

    <mx:ArrayCollection id="arrColl" source="{arr}" />

    <mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            alternatingItemColors="[white]"
            rowCount="6"
            horizontalGridLines="{hGL.selected}"
            verticalGridLines="{vGL.selected}"
            horizontalGridLineColor="red"
            verticalGridLineColor="red">
        <mx:columns>
            <mx:DataGridColumn id="labelCol" dataField="label" />
            <mx:DataGridColumn id="dataCol" dataField="data" />
        </mx:columns>
    </mx:DataGrid>

    <mx:CheckBox id="hGL" label="horizontalGridLines" selected="true" />
    <mx:CheckBox id="vGL" label="verticalGridLines" selected="true" />

</mx:Application>

View source is enabled in the following example.

 
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.

3 Responses to Toggling a DataGrid control’s horizontal grid lines and vertical grid lines

  1. Abhishek Tanwar says:

    Is there a way to increase the thickness of these lines? Basically my requirement is to have the horizontal and vertical padding between columns…

  2. jai says:

    Is there any way to get rid of vertical lines in the header?

  3. Amit says:

    To increase the thickness of these lines
    you can simply override the method

    override protected function drawVerticalLine
    and in that
    grahpics.lineStyle(line thickness, line color);

    let me know if any issue.
    Regards,
    Amit

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