2 Replies Latest reply on Feb 26, 2010 12:54 PM by PlumbSoldier

    Combobox as Datagrid Item editor

    PlumbSoldier

      Hi there, i'm trying to use a combobox as an itemEditor for a datagridcolumn, and i'm having some difficulties in doing this.

       

      I'm using the code below to define the column, so far so good the itemeditor appears and selects the item accordingly to the value of the datafield.

      My problem is, if I open the combobox and do not choose none of the items, the value that passes to the datagrid is 0, but i want to pass the old value. So that the value remains the same.

       

       

      the dataprodiver for the combobox is this one (rows from database)

      id description

      1     xxxxx

      2     yyyyy

       

      the datafield banda_horaria_id holds an integer witch match one of the values above.

       

      Can anyone help me, and tell me what should i do to maintain the value if none of the combobox items is selected.

       

      Thanks in advance.

      PlumbSoldier

       

      <mx:DataGridColumn headerText="Banda Horária" dataField="banda_horaria_id" id="bandaHoraria"
                              rendererIsEditor="false" editorDataField="selectedItem" >
                              <mx:itemEditor>
                                  <mx:Component>
                                      <mx:ComboBox initialize="outerDocument.cbBandaHoraria(event)"
                                              fontWeight="normal" labelField="descricao"  >
                                          <mx:Script>
                                              <![CDATA[
                                              import mx.controls.DataGrid;
                                              private var columnDataField:String;
                                              private var dtGrid:DataGrid;
                                                 
                                                   override public function set data(value:Object):void
                                                  {
                                                      dtGrid=listData.owner as DataGrid;
                                                      columnDataField=dtGrid.columns[listData.columnIndex].dataField;
                                                      super.data = value;
                                                      if (value != null)
                                                      {
                                                          var len:int = this.dataProvider.length;
                                                          for (var i:int = 0; i < len; i++)
                                                          {
                                                              if (this.dataProvider[i].id == value[columnDataField])
                                                              {
                                                                  this.selectedIndex = i;
                                                                 
                                                                  break;
                                                              }
                                                          }
                                                      }   
                                                  }        
                                                  public function onChange():void
                                                  {
                                                      var index:int = this.selectedIndex;
                                                      if (index !=-1){
                                                      id = this.dataProvider[index].id;
                                                      }  else{
                                                          var len:int = this.dataProvider.length;
                                                          for (var i:int = 0; i < len; i++)
                                                          {
                                                              if (this.dataProvider[i].id == data[columnDataField])
                                                              {
                                                                  this.selectedIndex = i;
                                                                 
                                                                  break;
                                                              }
                                                          }
                                                       
                                                 
                                                  }
                                      
                                             
                                              ]]>
                                          </mx:Script>
                                      </mx:ComboBox>
                                  </mx:Component>
                              </mx:itemEditor>
                          </mx:DataGridColumn>