1 Reply Latest reply on Sep 20, 2010 9:56 PM by BhaskerChari

    How to refresh a DataGrid from a different MXML component

    Devtron Level 3

      Hello

       

      I have the need to refresh a DataGrid from a different component than where the DataGrid resides.

       

      Component 1 has a datagrid.

      Component 2 has some UI input fields.

       

      I need component 2 to refresh the data in component 1's datagrid.

       

      How is this possible?

       

      if I do something like:

      Component1.datagrid.dataprovider = null;

       

      I get compiler errors. What is the safe way to access other components directly?

        • 1. Re: How to refresh a DataGrid from a different MXML component
          BhaskerChari Level 4

          Hi Devtron,

           

          You can figure this out by using events..

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete();" xmlns:comp="comp.*">
          <mx:Script>
            <![CDATA[
            
             private function onCreationComplete():void
             {
              comp2.addEventListener("refreshGridEvent",refreshGrid);
             }
             private function refreshGrid(event:Event):void
             {
              comp1.dataGrid.dataProvider = null;
             }
            ]]>
          </mx:Script>

           

          <comp:Component1 id="comp1" />
          <comp:Component2 id="comp2" />
          </mx:Application>

           

          Now in your Component2 dispatch the refreshGridEvent event either on a button click or when your UI elements changes based on your requirement...this.dispatchEvent(new Event('refreshGridEvent')); and listen for the evet in manin mxml file and change the dataProvider of the dataGrid in Component1.

           

          Note: This solution is if your both components are placed in the main mxml file. Is your structure is same as I mentioned in the code above..I mean

          whether your two components are placed in the main mxml file.

           

           

          Thanks,

          Bhasker