这是此帖子的延续,但添加了完整代码: 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();
}
}
}