3

This is a continuation of this posting, but adding full code: ASP.NET - GridView, adding header row in code

I am trying to add the <thead> and <tbody> tags to my gridview control.

Nothing I have tried is working. I continue to get the error:

The table must contain row sections in order of header, body, then footer.

Here is my 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 > 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();
}
}
}
3
  • ok now what exactly you want to do ? you want to set your gridview header column text ?? or anything else ? how you want your gridview look like ?? Commented Oct 3, 2013 at 11:57
  • I need to make sure that when the gridview renders, it adds the <theader> and <tbody> tags. Commented Oct 3, 2013 at 15:01
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Oct 3, 2013 at 16:53

1 Answer 1

6

The reason this is happening is because you have this setting in your Gridview, and it is trying to generate a Pager above the thead:

<PagerSettings Position="TopAndBottom" />

If you want to have a pager in the header and footer, you also have to set those Pagers to have be TableRowSection.TableHeader and TableRowSection.TableFooter respectively.

So use the following code:

gvShipments.UseAccessibleHeader = true;
gvShipments.HeaderRow.TableSection = TableRowSection.TableHeader;
if (gvShipments.TopPagerRow != null)
{
     gvShipments.TopPagerRow.TableSection = TableRowSection.TableHeader;
}
if (gvShipments.BottomPagerRow != null)
{
     gvShipments.BottomPagerRow.TableSection = TableRowSection.TableFooter;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to BRYAN! Awesome job sir.

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.