2 Replies Latest reply on Nov 26, 2007 4:37 PM by mike_morearty

    Adding rows to DataGrid

    Zolotoj Level 3
      Any samples of how to add new rows to a DataGrid?

      Thanks
        • 1. Re: Adding rows to DataGrid
          Zolotoj Level 3
          Ok, I have put some code together but it does not work.
          Any idea why?
          • 2. Re: Adding rows to DataGrid
            mike_morearty Level 1
            Here is a complete working sample that works. Your code sample isn't a complete sample, so I'm not quite sure which part caused your problem, but if I had to guess, I would bet the problem is caused by the fact that you did "addItem(Countries)" when you should have done "addItem(Countries.country)". Countries is an XML object whose root tag is <countries>; Countries.country is an E4X expression that resolves to an XMLList of tags, each of whose tagname is <country> (and in this particular case, that list will have just a single entry).

            Anyway, here's my working sample:

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" >
            <mx:Script>
            <![CDATA[
            public var Countries:XML =
            <countries>
            <country continent="Europe" countryName="Ukraine" article="test" />
            </countries>;
            ]]>
            </mx:Script>

            <mx:XML id="mydata" xmlns="">
            <countries>
            <country continent="North America" countryName="United States" article="test" />
            <country continent="North America" countryName="Canada" article="test" />
            </countries>
            </mx:XML>
            <mx:DataGrid id="grdAdvGrid" dataProvider="{mydata.country}">
            <mx:columns>
            <mx:DataGridColumn dataField="@continent" headerText="Continent" />
            <mx:DataGridColumn dataField="@countryName" headerText="Country" />
            </mx:columns>
            </mx:DataGrid>
            <mx:Button x="212" y="220" label="Add" click="grdAdvGrid.dataProvider.addItem(Countries.country)"/>
            </mx:Application>