title: GridView header text not showing when binded dynamically for first time
body: <p>I have a GridView, which is populated using a linq query on button click.  </p>
  
  <p>I have to populate the header text dynamically, fetching from the database. The first time I click the button, the header text is not binding. But, if I click on the button a second time, the header texts are bound.</p>
  
  <p>Markup:</p>
  
  <pre><code>   &lt;asp:GridView ID="grdresult" runat="server" AutoGenerateColumns="false" 
              OnRowDataBound="grdresult_RowDataBound"&gt;                   
              &lt;Columns&gt;                        
                  &lt;asp:TemplateField HeaderStyle-Width="10%"&gt;
                      &lt;ItemTemplate&gt;
                         &lt;asp:Label ID="lblCol1" runat="server" Text=' &lt;%#Eval("Col1") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField HeaderStyle-Width="10%"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Label ID="lblCol2" runat="server" Text='&lt;%#Eval("Col2") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField HeaderStyle-Width="10%"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Label runat="server" Text='&lt;%#Eval("Col3") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField HeaderStyle-Width="10%"&gt;
                      &lt;ItemTemplate&gt;
                         &lt;asp:Label ID="lblCol4" runat="server" Text=' &lt;%#Eval("Col4") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField HeaderStyle-Width="10%"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Label ID="lblCol5" runat="server" Text='&lt;%#Eval("Col5") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField HeaderStyle-Width="10%"&gt;
                      &lt;ItemTemplate&gt;
                         &lt;asp:Label ID="lblCol6" runat="server" Text=' &lt;%#Eval("Col6") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField HeaderStyle-Width="10%"&gt;
                      &lt;ItemTemplate&gt;
                         &lt;asp:Label ID="lblCol7" runat="server" Text=' &lt;%#Eval("Col7") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField HeaderStyle-Width="10%"&gt;
                      &lt;ItemTemplate&gt;
                         &lt;asp:Label ID="lblCol8" runat="server" Text=' &lt;%#Eval("Col8") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
              &lt;/Columns&gt;
              &lt;EmptyDataTemplate&gt;
              No Records Found
             &lt;/EmptyDataTemplate&gt;
          &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>Codebehind:</p>
  
  <pre><code>protected void btnreport_Click(object sender, EventArgs e)
  {       
      BindGrid();     
  }
  
  protected void BindGrid()
  {  
      var results = "LINQ select query from database";
                         }).Distinct().ToList();
  
      for (int i = 0; i &lt; grdresult.Columns.Count; i++)
      {
          grdresult.Columns[i].Visible = true;
      }
  
      grdresult.DataSource = results;
      grdresult.DataBind();       
  
  }
  
  protected void grdresult_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  
      if (e.Row.RowType == DataControlRowType.Header)
      {
          var getColumnHeader = "LINQ query to get header text";
          if (getColumnHeader != null)
          {
              grdresult.Columns[5].HeaderText = getColumnHeader.HCol3;
              grdresult.Columns[6].HeaderText = getColumnHeader.HCol4;
              grdresult.Columns[7].HeaderText = getColumnHeader.HCol5;
              grdresult.Columns[8].HeaderText = getColumnHeader.HCol6;
              grdresult.Columns[9].HeaderText = getColumnHeader.HCol7;
              grdresult.Columns[10].HeaderText = getColumnHeader.HCol8;
              grdresult.Columns[11].HeaderText = getColumnHeader.HCol9;
              grdresult.Columns[12].HeaderText = getColumnHeader.HCol10;                    
          }
      }
  
      //based on some condition i have set visibility of gridview columns
      int TotalColumnCount = (int)ViewState["colcount"];
      for (int i = grdresult.Columns.Count - 1; i &gt; TotalColumnCount + 4; i--)
      {
          grdresult.Columns[i].Visible = false;
      }        
  }
  </code></pre>