We help IT Professionals succeed at work.

How do I get a xml-named datagrid-column

petersego
petersego asked
on
Im trying to select a value from a specific column in a datagrid. That column is defined in a xml-file
firstColumn=dgXML.@firstColumn; // Column7

When I click an item in the datagrid, I want to select the content of that column.
e.target.selectedItem.firstColumn;
but it does not work even though everything works perfectedly, when referring the correct column like this.
e.target.selectedItem.Column7;
firstColumn has been set as String and is correctly traced just above
trace(firstColumn); // Column7
e.target.selectedItem.firstColumn; // undefined

What could be wrong






Comment
Watch Question

Top Expert 2011

Commented:
Are you using XmlListCollection how you defined DataGridColumn and dataProvider and why are you selecting by column name ?

Author

Commented:
I dont know of xmlListCollection - I think that is flex. Im in flash.
I dont think how Ive created the datagrid is important.
Let me rephrase the question.
How do i get the value from the first column of a specific row after clicking that row in the datagrid?
Top Expert 2011

Commented:
Will be faster for me to help you if you will attach your FLA file as CS4

Author

Commented:
Ive attached a CS4-file.
The textfield should show the value of the first column, but I can only make it show the object.

getdgvalue.fla
Top Expert 2011
Commented:
Sorry, I was busy but here is an answer
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
import fl.events.DataGridEvent;
import fl.events.ListEvent; 

var myXML:XML = <Root>
	<Row>
		<Column1>New Haven</Column1>
		<Column2>1003</Column2>
		<Column3>305</Column3>
		<Column4>118</Column4>
		<Column5>1426</Column5>
		<Column6>49503.25</Column6>
	</Row>
	<Row>
		<Column1>Mexico City</Column1>
		<Column2>829</Column2>
		<Column3>264</Column3>
		<Column4>114</Column4>
		<Column5>1207</Column5>
		<Column6>46630.25</Column6>
	</Row>
	<Row>
		<Column1>Shangri La</Column1>
		<Column2>790</Column2>
		<Column3>248</Column3>
		<Column4>114</Column4>
		<Column5>1152</Column5>
		<Column6>45585</Column6>
	</Row>
</Root>

var headerColArray:Array=new Array("City","Dogs","Cats","Horses","Apples");
var dataColArray:Array=new Array("Column1","Column2","Column3","Column4","Column5");

for(var i = 0; i < 5; i++){
	var cols:DataGridColumn = new DataGridColumn(dataColArray[i]);
	cols.headerText = headerColArray[i];
	dg.addColumn(cols);
}

dg.dataProvider = new DataProvider(myXML);
dg.addEventListener(ListEvent.ITEM_CLICK, itemChildEventHandler);  

function itemChildEventHandler(e:ListEvent):void { 
	var row:Object = e.target.getItemAt(e.rowIndex);
    var dgc1:DataGridColumn = dg.columns[0];
	var col1:String = row[dgc1.dataField];
	
	var dgc2:DataGridColumn = dg.columns[e.columnIndex];
	var col2:String = row[dgc2.dataField];
	var selectedCol:String = row[dgc2.dataField];
	firstColTxt.text = col1 + ", " + selectedCol;
} 

Open in new window

Author

Commented:
Two for one;-)
Thanks.
Absolutely perfect.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.