3 Replies Latest reply on Dec 30, 2009 8:21 AM by lvelicanu

    How do I change data in a datagrid?

    Jerry62712 Level 1

      A datagrid has data from a database call.  That data may have the literals "null" in various fields.  I would like to change the datagrid to blank out those literals.

       

      Background:

      An SQL database has a bunch of stuff in it from an Access database.  The user selects various criteria and we extract the data and use it in a dataprovider for a datagrid.

       

      Goal:

      Code to go through the entire datagrid looking for and changing any value that is "null" to blanks.

       

      Datagrid:

      <mx:FormItem id="lblSearchResults"
          label="Search Results"
          indicatorGap="0">
          <mx:DataGrid id="dgSearchResults"
              dataProvider="{dmSearch.RecProcess}"
              itemClick="dataGridClicked( event )"
              keyDown="dataGridPressed(event)"
              tabIndex="19"
              rowCount="7">
              <mx:columns>
                  <mx:DataGridColumn id="colLastName"
                      dataField="LASTNAME"
                      headerText="Last Name"
                      headerWordWrap="false" />
                  <mx:DataGridColumn id="colFirstName"
                      dataField="FIRSTNAME"
                      headerText="First Name"
                      headerWordWrap="false" />
                  <mx:DataGridColumn id="colMHDDID"
                      dataField="MHDDID"
                      headerText="MHDDID"
                      wordWrap="false" />
                  <mx:DataGridColumn id="colRIN"
                      dataField="RIN"
                      headerText="RIN"
                      wordWrap="false" />
                  <mx:DataGridColumn id="colSSN"
                      dataField="SSN"
                      headerText="SSN"
                      wordWrap="false" />
              </mx:columns>
          </mx:DataGrid>
      </mx:FormItem>

        • 1. Re: How do I change data in a datagrid?
          lvelicanu

          You could use an itemRenderer for all the columns in the dataGrid

           

          For example:

           

                     <mx:DataGridColumn id="colFirstName"
                          dataField="FIRSTNAME"
                          headerText="First Name"
                          headerWordWrap="false"

                          itemRenderer ="MyRenderer"/>

           

          and MyRenderer would display blank instead of null.

           

              public class MyRenderer extends Label
              {
                 
                  override public function set data(value:Object):void {
                      super.data = value;
                      if (value != null) {
                          this.text = value
                      } else {
                          // If value is null, clear text.
                          this.text= "";
                      }

           

                      super.invalidateDisplayList();
                  }
              }
          }

          1 person found this helpful
          • 2. Re: How do I change data in a datagrid?
            Jerry62712 Level 1

            Thanks for your reply.  I was leaning in that direction (with limited knowledge I wasn't leaning very hard) as the way to go.  I just didn't have any experience with that aspect.  I'm going to book mark that for future reference as it seems more like the way I wanted to go before I heard of "labelFunction".

            • 3. Re: How do I change data in a datagrid?
              lvelicanu Level 1

              You're right labelFunction is more simple to use in this case.

               

              In the DataGrid, for each column just add something like:

               

                   labelFunction="blankNullsFunc"

               

              and add this function

               

                   private function blankNullsFunc(item:Object, column:DataGridColumn):String  {


                        if (item[column.dataField] == null)
                             return "";

                        else
                             return item[column.dataField];

                   }