Lovcolumn.
Package class
{
Public class carvo
{
Public var name: string;
Public var available: Boolean;
Public Function carvo ()
{
}
}
}
Main. mxml
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" creationcomplete = "Init ()">
<Mx: SCRIPT>
<! [CDATA [
Import class. carvo;
Import class. lovcolumn;
Import MX. Collections. arraycollection;
Import MX. Controls. datagridclasses. datagridcolumn;
// Define an array of Dynamic Setting columns with the lovcolumn class content
Public var columnarray: arraycollection = new arraycollection ();
// Define the data source array
Public var sourcearray: arraycollection = new arraycollection ();
Private function Init (): void {
// Initialize the data source of the table columns
VaR lovcolumn: lovcolumn = new lovcolumn ();
Lovcolumn. columnname = "car name ";
Lovcolumn. datafield = "name ";
VaR lovcolumn2: lovcolumn = new lovcolumn ();
Lovcolumn2.columnname = "aviliable ";
Lovcolumn2.datafield = "available ";
Columnarray. additem (lovcolumn );
Columnarray. additem (lovcolumn2 );
// Initialize the table data source
VaR car: carvo = new carvo ();
Car. Name = "Ford ";
Car. Available = true;
VaR car1: carvo = new carvo ();
Car1.name = "Nissa ";
Car1.available = true;
VaR car2: carvo = new carvo ();
Car2.name = "Buick ";
Car2.available = false;
Sourcearray. additem (CAR );
Sourcearray. additem (car1 );
Sourcearray. additem (car2 );
For (var I: Number = 0; I <columnarray. length; I ++ ){
VaR column: Maid = new maid ();
Column. headertext = columnarray [I]. columnname;
Column. datafield = columnarray [I]. datafield;
// You must add columns as follows. Otherwise, the display will be abnormal. For details, see:
// Http://q.sohu.com/forum/5/topic/2275790
DG. Columns = DG. Columns. Concat (column );
}
DG. dataprovider = sourcearray;
}
]>
</MX: SCRIPT>
<Mx: DataGrid x = "344" Y = "125" id = "DG">
</MX: DataGrid>
</MX: Application>
The next goal is to make it a common component.