title: Silverlight Gridcolumn Collapse Visibility in code
body: <p>I'm having a small problem.</p>
  
  <p>I have this grid with a column:</p>
  
  <pre><code>&lt;data:DataGrid ColumnHeaderStyle="{StaticResource headerStyle}" Foreground="#234BC3" AutoGenerateColumns="False" Name="protocollenBAMDataGrid" LoadingRow="myDataGrid_LoadingRow" SelectionChanged="DataGrid_SelectionChanged"&gt;
                      &lt;data:DataGrid.Columns&gt;
                          &lt;data:DataGridTemplateColumn Header="Resend" x:Name="ResendColumn"&gt;
                              &lt;data:DataGridTemplateColumn.CellTemplate&gt;
                                  &lt;DataTemplate&gt;
                                      &lt;Button Content="Resend" Tag="{Binding MsgID}" Foreground="#234BC3" Click="dataGridHL7_Click"&gt;&lt;/Button&gt;
                                  &lt;/DataTemplate&gt;
                              &lt;/data:DataGridTemplateColumn.CellTemplate&gt;
                          &lt;/data:DataGridTemplateColumn&gt; ...
  </code></pre>
  
  <p>If i set the visibility of the "ResendColumn" to collapsed, it works fine.</p>
  
  <p>However I need to do this in code, so I tried doing this in the loading event:</p>
  
  <pre><code>ResendColumn.Visibility = Visibility.Collapsed;
  </code></pre>
  
  <p>And it gives me a nullref exception:</p>
  
  <blockquote>
    <p>Object reference not set to an
    instance of an object.</p>
  </blockquote>
  
  <p>I don't understand why?</p>