views:

20

answers:

1

i want to make a button disabled if a datagrid is empty and it should be enabled when there is atleast 1 entry.the entries in the grid are made at runtime.I tried this this is the button:

<mx:Button id="update" label="Update Contact" enabled="{isButtonEnabled()}"/>

and the function is defined as where dg_contact is the datagrid:

public function isButtonEnabled():Boolean
{
     if(dg_contact.selectedIndex==-1)
    {
        return false;
    }
    else
    {
        return true;
    }
}

where am i going wrong?

+1  A: 

Your code doesn't work because isButtonEnabled() doesn't get called when selectedIndex changes. You can use BindingUtils to do that, but this can be done without BindingUtils

DataGrid can have items and yet have its selectedIndex equal to -1. If you're not bothered about if an item is selected or not, bind it to the length of DataGrid's dataProvider

<mx:Button id="update" label="Update Contact" 
               enabled="{dg_contact.dataProvider.length != 0}"/>

If you want button to be enabled only when something is selected, bind it to selectedIndex

<mx:Button id="update" label="Update Contact" 
               enabled="{dg_contact.selectedIndex != -1}"/>
Amarghosh
thanx it worked just perfectly
ruchir patwa