DataGrid columns from data

Ask a Question related to Macromedia Flex General Discussion, Design and Development.

  1. #1

    Default DataGrid columns from data

    I posted this under the FlexBuilder topic then realized it is more appropriate
    here.

    I noticed in the database sample program, the results of the SQL queries
    populated the data grid based on the columns available in the tables. How can
    I dynamically create datagrid columns based on the supplied data?

    Kwooda Guest

  2. Similar Questions and Discussions

    1. Dynamic columns in DataGrid, Paging/Sorting and Data Caching
      Hi, I have a DataGrid that has no columns and that is populated with columns based on data obtained from DB during PageLoad in the following way:...
    2. binding data to datagrid(template columns)
      I create a datagrid where only have 2 template columns(label control) . I want to populate them from a dataset , but i donīt know how to do it??...
    3. Columns and Inherited Datagrid...Active Schema does not support columns
      I have a class which has inherited from datagrid, to provide some custom functionality, row select, mouse overs etc All is working fine apart from...
    4. Datagrid Template columns shows data from first row
      I have a datagrid with a template column that has a hyperlink and a label. The hyperlink text is bound to Title from my dataset and the label text...
    5. Pad strings/concatenate columns to simulate data columns in ASP select box with SQL Server 2K
      Dale, Cast all the data to fixed character data type in your select statement. For example: select cast(au_id as char(12)) +...
  3. #2

    Default Re: DataGrid columns from data

    Ok, even though you didn't state the Flex version, I'll respond anyway, but
    please, in the future state the version in the subject.

    Here is a sample app that dynamically creates datagrid columns based on xml
    data. It is self contained, with the sample xml declared in line, but a real
    app would load the column definitions from an external source.

    Let us know if you have any questions.

    Tracy


    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
    initialize="initApp()">
    <mx:Script><![CDATA[
    var _xmlColumnDef:XML;

    var _aDP:Array;

    private function initApp():Void
    {
    var sXML:String = "";
    //hardcoded/simulate column def could be external xml
    sXML += "<columndefs>";
    sXML += '<columndef columnName="col1" headerText="Column One" width="100"
    />';
    sXML += '<columndef columnName="col2" headerText="Column Two" width="200">';
    sXML += "</columndefs>";

    //simulate a dataprovider
    _aDP = new Array({col1:"row 1 col 1", col2:"row 1 col 2"},{col1:"row 2 col
    1", col2:"row 2 col 2"});
    buildDataGridColumns(sXML);
    }

    private function buildDataGridColumns(sXML:String):Void
    {
    var sColumnName:String;
    var sHeaderText:String;
    //var sWidth:String;
    var xmlColDef:XML = mx.utils.XMLUtil.createXML(sXML);
    var aColDefNodes:Array = xmlColDef.firstChild.childNodes;
    var nodeColDef:XMLNode;
    for ( var i:Number=0; i<aColDefNodes.length; i++ ) {
    nodeColDef = aColDefNodes[i]; //get the current node
    sColumnName = nodeColDef.attributes.columnName; //get the properties
    sHeaderText = nodeColDef.attributes.headerText;
    //sWidth = nodeColDef.attributes.width;
    var dgcNew = new mx.controls.gridclasses.DataGridColumn(); //now create the
    column
    dgDynamic.addColumn(dgcNew); //add the column to the dg
    dgcNew.columnName = sColumnName; //set the properties
    dgcNew.headerText = sHeaderText;
    //dgcNew.width = sWidth; //THIS DOES NOT WORK AND BREAKS THE FUNCTION
    }//for ( var i:Number=0;....
    onResultSimulated({result: _aDP}) //simulate calling the result handler
    }

    private function onResultSimulated(oEvent:Object):Void
    {
    dgDynamic.dataProvider = oEvent.result
    }
    ]]></mx:Script>

    <mx:DataGrid id="dgDynamic" />

    </mx:Application>

    ntsiii Guest

  4. #3

    Default Re: DataGrid columns from data

    My example is 1.5, but it might still be useful.
    Tracy
    ntsiii Guest

  5. #4

    Default Re: DataGrid columns from data

    Thanks, Tracy, though the object hierarchy seems to have significantly changed.
    I can't find anything comparable to the addColumn() method, but at least I
    have a structure to guide me. I'll keep at it.

    Kwooda Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139