0

I've scoured the web looking at the various examples and have tried every single one of them. I get the same error no matter what. I am trying to add the headerrow to the GridView control in code. I have tried adding the below code in every possible event from gridview prerender to all of the events of the page. Same deal. Always get the error: The table must contain row sections in order of header, body, then footer.

I have stripped down the page to the bare essentials - removing the master page and all CSS.

Here is the aspx and grid view code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Shipping.Admin.Default" Title="Apps - Shipping" %>
<html>
<head><title></title></head>
<body>
<form runat="server" id="form1">
<br />
<h1>Admin Page</h1>
<br />

            <asp:GridView ID="gvShipments" runat="server" AllowSorting="True" 
                AutoGenerateColumns="False" onsorting="gvShipments_Sorting" Width="100%" 
                AllowPaging="True" onpageindexchanging="gvShipments_PageIndexChanging" 
                PageSize="50">                 
                 <PagerSettings Position="TopAndBottom" />
                <Columns>
                    <asp:BoundField DataField="RequestDate" HeaderText="Request Date" SortExpression="dtRequestDate" />
                    <asp:BoundField DataField="Requestor" HeaderText="Requestor" SortExpression="Requestor" />
                    <asp:BoundField DataField="CompanyName" HeaderText="Company" SortExpression="CompanyName" />
                    <asp:BoundField DataField="ShipmentDescription" HeaderText="Description" SortExpression="ShipmentDescription" />
                    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                    <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
                    <asp:BoundField DataField="ShipmentType" HeaderText="Shipment Type" SortExpression="ShipmentType" />
                    <asp:BoundField DataField="ServiceLevel" HeaderText="Service Level" SortExpression="ServiceLevel" />
                    <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
                    <asp:HyperLinkField  DataNavigateUrlFields="ShipmentId" DataNavigateUrlFormatString="Shipment.aspx?CatId=Admin&amp;sID={0}" Text="&nbsp;&nbsp;&nbsp;edit" />
               </Columns>
           </asp:GridView>       
<br />
</form>
</body>
</html>

Here is a snipped of the code-behind. This method is called in the Page_Load method:

private void LoadGridView()
        {
            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";
            }

            gvShipments.DataSource = dv;
            gvShipments.DataBind();

            **if (gvShipments.Rows.Count > 0)
            {
                this.gvShipments.UseAccessibleHeader = true;
                this.gvShipments.HeaderRow.TableSection = TableRowSection.TableHeader;
            }**    

            ds.Dispose();
        }

I have tried adding the code inside bolded the IF statement everywhere. I am stumped. Please help!

Thanks

7
  • you want to set header text ?? Commented Oct 1, 2013 at 14:38
  • I want to force <thead> and <tbody> to be rendered in the gridview. Commented Oct 1, 2013 at 14:49
  • Why do you need them? And if you do - cant you add them in client-side code? Commented Oct 1, 2013 at 15:40
  • It's required with a jQuery library I need to use. How would I add them in client side code? Commented Oct 1, 2013 at 15:42
  • Do you require any additional code from the code-behind to make a proper evaluation? I'm hoping some ASP.NET guru out there can assist. Commented Oct 1, 2013 at 17:59

1 Answer 1

2

Try this add Page_PreRender event and post your code inside it, then your code look like as below

protected void Page_PreRender(object sender, EventArgs e)
 {
     if (gvShipments.Rows.Count > 0)
     {
         gvShipments.UseAccessibleHeader = true;
         gvShipments.HeaderRow.TableSection = TableRowSection.TableHeader;
     }
}

PageLifeCycle enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: The table must contain row sections in order of header, body, then footer. [ stack trace omitted] Version Information: Microsoft .NET Framework Version:2.0.50727.5472; ASP.NET Version:2.0.50727.5471
see how many records are their in your dataview and have you used Page_PreRender event
you can add this in your question , it would be easily readable for us, or create new question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.