title: ASP.NET: When and how to dynamically change Gridview's headerText's in code behind?
body: <p>I have a gridview with 2 columns. I want to learn coding behind and <strong>do NOT want to do this in the aspx file.</strong>
  How do I set the header text for my columns dynamically? At what point do I do so? After the adapter has filled the gridview with data?
  Right now, I have the header text but it is exactly the same as the datafield name which is last_name and I want to see Last Name in the header field instead.
  I've tried </p>
  
  <pre><code>GridView1.Columns[0].HeaderText = "Last Name";
  </code></pre>
  
  <p>but wherever I tried to put it, the compiler complains about index out of range.</p>
  
  <p>Thanks.</p>
  
  <p>aspx code for the gridview:</p>
  
  <pre><code>    &lt;asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                  BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"
                  Width="728px" CellPadding="4" ForeColor="Black" GridLines="Vertical" OnPageIndexChanging="GridView1_PageIndexChanging"
                  OnSorting="GridView1_Sorting" PageSize="14" OnRowDataBound="GridView1_RowDataBound"&gt;
                  &lt;AlternatingRowStyle BackColor="White" /&gt;
                  &lt;FooterStyle BackColor="#CCCC99" /&gt;
                  &lt;HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" /&gt;
                  &lt;PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" /&gt;
                  &lt;RowStyle BackColor="#F7F7DE" /&gt;
                  &lt;SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /&gt;
                  &lt;SortedAscendingCellStyle BackColor="#FBFBF2" /&gt;
                  &lt;SortedAscendingHeaderStyle BackColor="#848384" /&gt;
                  &lt;SortedDescendingCellStyle BackColor="#EAEAD3" /&gt;
                  &lt;SortedDescendingHeaderStyle BackColor="#575357" /&gt;
                  &lt;PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last"
                      PageButtonCount="5" Position="Bottom" /&gt;
              &lt;/asp:GridView&gt;
  </code></pre>