<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>FlexOOP &#187; array</title>
	<atom:link href="http://flexoop.com/tag/array/feed/" rel="self" type="application/rss+xml" />
	<link>http://flexoop.com</link>
	<description>Flex, AIR, ColdFusion, and everything in between</description>
	<lastBuildDate>Tue, 13 Jul 2010 03:56:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using ActionScript to set DataGrid columns</title>
		<link>http://flexoop.com/2008/07/using-actionscript-to-set-datagrid-columns/</link>
		<comments>http://flexoop.com/2008/07/using-actionscript-to-set-datagrid-columns/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 17:41:27 +0000</pubDate>
		<dc:creator>Gareth</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[array]]></category>

		<guid isPermaLink="false">http://flexfusion.archfamily.com/?p=19</guid>
		<description><![CDATA[If you are setting a datagrid&#8217;s columns using mxml, it is a very simple process: &#60;mx:DataGrid id=&#34;myDataGrid&#34; dataProvider=&#34;{ myData }&#34;&#62; &#60;mx:columns&#62; &#60;mx:DataGridColumn dataField=&#34;col1&#34; headerText=&#34;Column 1&#34; /&#62; &#60;mx:DataGridColumn dataField=&#34;col2&#34; headerText=&#34;Column 2&#34; /&#62; &#60;/mx:columns&#62; &#60;/mx:DataGrid&#62; However, if you are setting the columns via ActionScript, it is also somewhat straightforward but requires a slight variation. If I were [...]]]></description>
			<content:encoded><![CDATA[<p>If you are setting a datagrid&#8217;s columns using mxml, it is a very simple process:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>mx:DataGrid id=<span style="color: #ff0000;">&quot;myDataGrid&quot;</span> dataProvider=<span style="color: #ff0000;">&quot;{ myData }&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>mx:columns<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>mx:DataGridColumn dataField=<span style="color: #ff0000;">&quot;col1&quot;</span> headerText=<span style="color: #ff0000;">&quot;Column 1&quot;</span> <span style="color: #66cc66;">/&gt;</span>
        <span style="color: #66cc66;">&lt;</span>mx:DataGridColumn dataField=<span style="color: #ff0000;">&quot;col2&quot;</span> headerText=<span style="color: #ff0000;">&quot;Column 2&quot;</span> <span style="color: #66cc66;">/&gt;</span>
    <span style="color: #66cc66;">&lt;/</span>mx:columns<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>mx:DataGrid<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>However, if you are setting the columns via ActionScript, it is also somewhat straightforward but requires a slight variation.</p>
<p>If I were to set the same columns on my datagrid above in ActionScript, you would perhaps first think that it would be something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myDataGridColumn:DataGridColumn = <span style="color: #000000; font-weight: bold;">new</span> DataGridColumn<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;col1&quot;</span> <span style="color: #66cc66;">&#41;</span>;
myDataGridColumn.<span style="color: #006600;">headerText</span> = <span style="color: #ff0000;">&quot;Column 1&quot;</span>;
myDataGrid.<span style="color: #006600;">columns</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> myDataGridColumn <span style="color: #66cc66;">&#41;</span>;
myDataGridColumn = <span style="color: #000000; font-weight: bold;">new</span> DataGridColumn<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;col2&quot;</span> <span style="color: #66cc66;">&#41;</span>;
myDataGridColumn.<span style="color: #006600;">headerText</span> = <span style="color: #ff0000;">&quot;Column 2&quot;</span>;
myDataGrid.<span style="color: #006600;">columns</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> myDataGridColumn <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This, however, does not work.  The columns get overwritten and the new columns do not show up correctly.</p>
<p>The actual method for setting columns is to first set them to a separate array:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> columns:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">var</span> myDataGridColumn:DataGridColumn = <span style="color: #000000; font-weight: bold;">new</span> DataGridColumn<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;col1&quot;</span> <span style="color: #66cc66;">&#41;</span>;
myDataGridColumn.<span style="color: #006600;">headerText</span> = <span style="color: #ff0000;">&quot;Column 1&quot;</span>;
columns.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> myDataGridColumn <span style="color: #66cc66;">&#41;</span>;
myDataGridColumn = <span style="color: #000000; font-weight: bold;">new</span> DataGridColumn<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;col2&quot;</span> <span style="color: #66cc66;">&#41;</span>;
myDataGridColumn.<span style="color: #006600;">headerText</span> = <span style="color: #ff0000;">&quot;Column 2&quot;</span>;
columns.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> myDataGridColumn <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Then from here, you set the dataGrid columns array to the newly created array of columns:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">myDataGrid.<span style="color: #006600;">columns</span> = columns;</pre></div></div>

<p>This will then force the datagrid to use the columns you have set rather than just using the properties from the dataprovider to create the column names.</p>
<p>This is about the 3rd time I&#8217;ve forgotten about this twist, so hopefully by writing a blog entry about it, it will solidify it for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://flexoop.com/2008/07/using-actionscript-to-set-datagrid-columns/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
