这是此帖子的延续,但添加完整代码: ASP.NET - GridView, adding header row in code
我正在尝试将<thead>
和<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();
}
}
}
答案 0 :(得分:3)
发生这种情况的原因是因为您在Gridview中有此设置,并且它正在尝试在thead上方生成一个寻呼机:
<PagerSettings Position="TopAndBottom" />
如果您想在页眉和页脚中添加寻呼机,则还必须将这些寻呼机分别设置为TableRowSection.TableHeader
和TableRowSection.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;
}