If it is working for you good, but that may be the hard way. My steps to this are:
* bind the form fields to the selectedItem * bind the DG to an instance var * In the data service result handler, assign the result to the instance var, then use callLater() to select the first row. Tracy ________________________________ From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of guitarguy555 Sent: Tuesday, March 04, 2008 7:57 PM To: [email protected] Subject: [flexcoders] Re: DataGrid - programmatically selecting the first row - selectedItem is null Thanks Nayan, The updateComplete event allowed me to access the data. Again, here are my requirements: - DataGrid bound to XML which is loaded from an HttpService object - I want to automatically select the first row in the grid when the application loads. - I want to populate form values with the XML data from that first selected row. So in order to do this: in the App's creationComplete event handler I select that first row: dg.selectedIndex = 0; dg.validateNow(); dg.scrollToIndex(0); Then in the DataGrid's updateComplete event I have to check to see if the data is of type XMLList and also check a boolean to see if this is the first time the app has loaded. The data won't be of type XMLList until after the HttpService's result event fires and that XML data is assigned to the DataGrid. private function dgUpdateComplete(event:FlexEvent):void{ if((!(event.target.dataProvider is ArrayCollection) && selectFirstRowOnLoad == true))){ // Note that event.target.dataProvider is of type ArrayCollection before the datagrid has been assigned the XML data. // Populate form controls for first row dg.selectedItem = event.target.dataProvider[0]; populateForm(event.target.dataProvider[0]); // function to fill form controls selectFirstRowOnLoad = false; // set class level boolean to false } } Hope this helps anyone else out there that has a similar requirement. --- In [email protected] <mailto:flexcoders%40yahoogroups.com> , "Nayan Savla" <[EMAIL PROTECTED]> wrote: > > i used updateComplete to listen for changes in the dataProvider. > > private function categoryDataChanged(event:FlexEvent):void{ > if(event.currentTarget == categoryCombo){ > //this is to prevent it from collecting garbage data for the first time > try { > if (event.target.selectedItem.data != -1) { > > } > } catch(e:Error){ > //do nothing > } > > } > } > > this is how my listener is implemented, i had to use try block because > the first time the value would be null and i wouldn't get error in > flex. > > Nayan > On Fri, Feb 29, 2008 at 4:03 PM, Tracy Spratt <[EMAIL PROTECTED]> wrote: > > > > > > > > > > > > > > > > > > > > I have found that whenever you programatically assign a dataProvider, you > > must use callLater() to invoke any methods that affect the visual UI. > > > > > > > > Tracy > > > > > > > > ________________________________ > > > > > > From: [email protected] <mailto:flexcoders%40yahoogroups.com> [mailto:[email protected] <mailto:flexcoders%40yahoogroups.com> ] On > > Behalf Of guitarguy555 > > Sent: Friday, February 29, 2008 3:50 PM > > To: [email protected] <mailto:flexcoders%40yahoogroups.com> > > Subject: [flexcoders] Re: DataGrid - programmatically selecting the first > > row - selectedItem is null > > > > > > > > > > > > > > Thanks for the replies - well, I tried that suggestion ( > > dg.selectedItem = xmlData[0] )and the XML is null at this point. > > > > Yes, the XML data is not available yet when the App's creation > > complete is called. I should have mentioned as well that this > > DataGrid and form are in a component. The component's DataGrid data > > source is set from the mxml declaration in the application. > > > > The XML data is retrieved from an HTTPSService object. So in the > > main App, once this XML data is retrieved, I now try to set the > > selectedItem as follows in the result handler: > > > > private function onHandleGetData(event:ResultEvent):void{ > > > > tableData = event.result as XML; > > dg.dgSelector.selectedItem = tableData[0]; > > > > } > > > > When I step through the code, I can see that the result contains the > > desired XML. But when I step into the code on that last line - where > > I try to assign a value to dg.selectedItem, Flex ends up in the > > ListBase.as clearSelected() function. > > > > In this function these 2 lines get called: > > > > _selectedIndex = -1; > > _selectedItem = null; > > > > which cancel out the assignment!! > > > > Has anybody else run into this situation? > > > > I just want to programmatically select that first row and have the > > corresponding form populate with that row's values. > > > > Given that the Grid and form are in a component, this might > > complicate things but it seems to me that this would be a common > > scenario - having a master/detail Grid/Form in a component. > > > > Thanks > > > > --- In [email protected] <mailto:flexcoders%40yahoogroups.com> , "letterpigeon" <ban.luc@> wrote: > > > > > > I think this might be a race condition problem. Your data might not > > > have been loaded when the creationComplete event is fired (it is > > fired > > > after all the UI components are created, but any service call you > > made > > > to load up the data might not have returned yet). > > > > > > Ban > > > > > > --- In [email protected] <mailto:flexcoders%40yahoogroups.com> , "guitarguy555" <djohnson29@> > > wrote: > > > > > > > > In my application, I have a datagrid that is bound to XML data. > > The > > > > datagrid has a corresponding form that contains the details. > > When > > > > the user clicks on a row in the datagrid the form populates with > > the > > > > values from the DataGrid's selectedItem. > > > > > > > > I want to programatically select the first row in my DataGrid > > when > > > > the application loads. > > > > > > > > In order to do this, I have to call validateNow() and > > scrollToIndex() > > > > > > > > eg. > > > > > > > > dg.selectedIndex = 0; > > > > dg.validateNow(); > > > > dg.scrollToIndex(0); > > > > > > > > I put this code in the Applications creationComplete handler. > > > > > > > > This all works great - the first row is highlighted and selected > > and > > > > the selectedIndex is 0. The problem is that I can't access the > > data > > > > to populate the details form. When I try to retrieve the > > > > dg.SelectedItem property - it is null. > > > > > > > > How does one programatically select the first row in the grid on > > load > > > > AND access the row data? > > > > > > > > thanks > > > > > > > > > > > >

