title: GridView, adding header row in code Part 2
body: <p>This is a continuation of this posting, but adding full code:
  <a href="https://stackoverflow.com/questions/19119004/asp-net-gridview-adding-header-row-in-code">ASP.NET - GridView, adding header row in code</a></p>
  
  <p>I am trying to add the <code>&lt;thead&gt;</code> and <code>&lt;tbody&gt;</code> tags to my <code>gridview</code> control.  </p>
  
  <p>Nothing I have tried is working.  I continue to get the error: </p>
  
  <pre><code>The table must contain row sections in order of header, body, then footer.
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>public partial class Default : System.Web.UI.Page
  {
  
  protected void Page_Load(object sender, EventArgs e)
  {   
  LoadGridView();
  }
  
  protected DataView GetDataSource()
  {
  DataSet ds = new DataSet();
  ds = GetDataset();
  
  DataTable dtRequests = ds.Tables["Admin"];
  DataView dv = new DataView(dtRequests);
  
  if (ViewState["sortexpression"] != null)
  {
  dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
  }
  else
  {
  dv.Sort = "dtRequestDate DESC";
  }
  
  ds.Dispose();
  return dv;
  }
  
  private void LoadGridView()
  {   
  gvShipments.DataSource = GetDataSource();
  gvShipments.DataBind();
  }
  
  protected void Page_PreRender(object sender, EventArgs e)
  {
  if (gvShipments.Rows.Count &gt; 0)
  {
  gvShipments.UseAccessibleHeader = true;
  gvShipments.HeaderRow.TableSection = TableRowSection.TableHeader;
  }
  }
  
  private DataSet GetDataset()
  {
  Tools oTools = new Tools();
  
  SqlConnection conn = new     SqlConnection(csShipping);
  SqlCommand cmd = new SqlCommand();
  
  cmd.CommandText = "dbo.[cGetOpenShipments]";
  cmd.CommandType = CommandType.StoredProcedure;
  cmd.Connection = conn;
  cmd.Connection.Open();
  
  SqlDataAdapter da = new SqlDataAdapter();
  DataSet ds = new DataSet();
  
  da.SelectCommand = cmd;
  da.Fill(ds, "Admin");
  
  cmd.Connection.Close();
  
  da.Dispose();
  cmd.Dispose();
  conn.Dispose();
  
  return ds;
  }   
  
  protected void gvShipments_Sorting(object sender, GridViewSortEventArgs e)
  {
  ViewState["sortexpression"] = e.SortExpression;
  
  if (ViewState["sortdirection"] == null)
  {
  ViewState["sortdirection"] = "asc";
  }
  else
  {
  if (ViewState["sortdirection"].ToString() == "asc")
  {
  ViewState["sortdirection"] = "desc";
  }
  else
  {
  ViewState["sortdirection"] = "asc";
  }
  }
  LoadGridView();
  }
  
  protected void gvShipments_PageIndexChanging(object sender,    GridViewPageEventArgs e)
  {
  gvShipments.PageIndex = e.NewPageIndex;
  LoadGridView();
  }
  }
  }
  </code></pre>