In a previous example, “Auto-scrolling a TextArea control in Flex”, we saw how you can auto-scroll a Flex TextArea control when new content is added by setting the verticalScrollPosition property to the value of the maxVerticalScrollPosition property.
The following example shows how you can auto-scroll a Flex DataGrid control using the ArrayCollection object’s collectionChange event, and the verticalScrollPosition and maxVerticalScrollPosition properties.
<?xml version="1.0" encoding="utf-8"?> <!--http://blog.flexexamples.com/2009/01/31/creating-an-auto-scrolling-datagrid-control-in-flex/ --> <mx:Application name="DataGrid_maxVerticalScrollPosition_text" xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="init();"> <mx:Script> <![CDATA[ import mx.events.CollectionEvent; private var timer:Timer; private function init():void { timer = new Timer(500); timer.addEventListener(TimerEvent.TIMER, onTimer); timer.start(); } private function onTimer(evt:TimerEvent):void { var now:String = new Date().toTimeString(); arrColl.addItem({id:timer.currentCount, time:now}); } private function arrColl_collectionChange(evt:CollectionEvent):void { callLater(autoScrollDataGrid); } private function autoScrollDataGrid():void { if (dataGrid) { dataGrid.validateNow(); dataGrid.verticalScrollPosition = dataGrid.maxVerticalScrollPosition; } } ]]> </mx:Script> <mx:ArrayCollection id="arrColl" collectionChange="arrColl_collectionChange(event);" /> <mx:DataGrid id="dataGrid" dataProvider="{arrColl}" verticalScrollPolicy="on" width="200" rowCount="8"> <mx:columns> <mx:DataGridColumn dataField="id" width="50" /> <mx:DataGridColumn dataField="time" /> </mx:columns> </mx:DataGrid> </mx:Application>
View source is enabled in the following example.



{ 7 comments… read them below or add one }
Why creator of this great website has stopped adding flex SWFs to posts ?
Creator is lazy, and didn’t have time to create Builder projects, export release builds, FTP files, link examples, etc.
But, on a positive note, I have been going back through older Flex 4 examples and updating them to the latest API and in several cases added SWFs.
Peter
SWF added. Go me!
I think it’s not necessarily?, in some simple cases.
me too
thanks
Feel free to check out http://www.flexicious.com. It does a lot of stuff in addition to what’s provided by Flex out of the box. Examples are Column level filtering, Server based paging and sorting, column footers, data copy paste, and a lot more!
How to auto scroll a list component vertically with itemrenderer as image