2

这是此帖子的延续,但添加了完整代码: ASP.NET - GridView,在代码中添加标题行

我正在尝试将<thead>and<tbody>标签添加到我的gridview控件中。

我没有尝试过任何工作。我继续收到错误:

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

这是我的代码:

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();
}
}
}
4

1 回答 1

5

发生这种情况的原因是因为您在 Gridview 中有此设置,并且它试图在 thead 上方生成一个寻呼机:

<PagerSettings Position="TopAndBottom" />

如果你想在页眉和页脚中有一个寻呼机,你还必须将这些寻呼机分别设置为 beTableRowSection.TableHeaderTableRowSection.TableFooter

所以使用下面的代码:

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;
}
于 2013-10-03T15:48:31.400 回答