1
votes

I am trying to make a datagrid, that will dynamically add columns to it based on some condition. Now, I am able to add the columns, but I want the newly added column to have button using itemRenderer.

I am unable to achieve this though. Getting this error on line 1

Description Resource Path Location Type 1067: Implicit coercion of a value of type mx.controls:Button to an unrelated type mx.core:IFactory. Demo.mxml /Demo/src line 14 Flex Problem

Can anyone help ?

Here's a code snippet :

private function addDataGridColumn(dataField:String):void {
            var dgc:DataGridColumn = new DataGridColumn();
            dgc.itemRenderer = button1;    // Line 1 
            var cols:Array = dataGrid.columns;
            cols.push(dgc);
            dataGrid.columns = cols;
        }
1
Can you show the declaration and intialization of button1?Jacob Eggers

1 Answers

4
votes

The itemRenderer and itemEditor properties are of type IFactory. When you set these properties in MXML, the MXML compiler automatically casts the property value to the type ClassFactory, a class that implements the IFactory interface.

When you set these properties in ActionScript, you must explicitly cast the property value to ClassFactory

You might be looking for this, adds buttons to all rows of newly added column.

private function addDataGridColumn(dataField:String):void {
                var dgc:DataGridColumn = new DataGridColumn();
                dgc.itemRenderer = new ClassFactory(Button);
                var cols:Array = dataGrid.columns;
                cols.push(dgc);
                dataGrid.columns = cols;
            }