GridView控件,添加标题行中code第2部分 [英] GridView, adding header row in code Part 2

查看:102
本文介绍了GridView控件,添加标题行中code第2部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此张贴延续,但加入全code:
<一href=\"http://stackoverflow.com/questions/19119004/asp-net-gridview-adding-header-row-in-$c$c\">ASP.NET - GridView控件,在code 添加标题行

我试图添加&LT; THEAD&GT; &LT; TBODY&GT; 标签来我 GridView控件控制。

没有什么我都试过工作。我继续得到错误:

 表必须包含在头部,身体的定单行部分,然后页脚。

下面是我的code:

 公共部分类默认:System.Web.UI.Page
{保护无效的Page_Load(对象发件人,EventArgs的发送)
{
LoadGridView();
}保护数据视图了getDataSource()
{
DataSet的DS =新的DataSet();
DS = getDataSet()返回;数据表dtRequests = ds.Tables [管理员];
DataView的DV =新的数据视图(dtRequests);如果(的ViewState [索特克斯pression]!= NULL)
{
。dv.Sort =的ViewState [索特克斯pression]的ToString()++的ViewState [sortdirection]的ToString()。
}
其他
{
dv.Sort =dtRequestDate DESC;
}ds.Dispose();
回到DV;
}私人无效LoadGridView()
{
gvShipments.DataSource =了getDataSource();
gvShipments.DataBind();
}保护无效Page_ preRender(对象发件人,EventArgs的发送)
{
如果(gvShipments.Rows.Count大于0)
{
gvShipments.UseAccessibleHeader = TRUE;
gvShipments.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}私人数据集getDataSet()返回
{
工具oTools =新工具();康涅狄格州的SqlConnection =新的SqlConnection(csShipping);
CMD的SqlCommand =新的SqlCommand();cmd.CommandText =DBO [cGetOpenShipments];
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection =康恩;
cmd.Connection.Open();SqlDataAdapter的大=新的SqlDataAdapter();
DataSet的DS =新的DataSet();da.SelectCommand = CMD;
da.Fill(DS,管理);cmd.Connection.Close();da.Dispose();
cmd.Dispose();
conn.Dispose();返回DS;
}保护无效gvShipments_Sorting(对象发件人,GridViewSortEventArgs E)
{
的ViewState [索特克斯pression] = e.SortEx pression;如果(的ViewState [sortdirection] == NULL)
{
的ViewState [sortdirection] =ASC;
}
其他
{
如果(的ViewState [sortdirection]。的ToString()==ASC)
{
的ViewState [sortdirection] =说明;
}
其他
{
的ViewState [sortdirection] =ASC;
}
}
LoadGridView();
}保护无效gvShipments_PageIndexChanging(对象发件人,GridViewPageEventArgs E)
{
gvShipments.PageIndex = e.NewPageIndex;
LoadGridView();
}
}
}


解决方案

这是发生的原因是因为你有你的GridView此设置,它正试图生成以上寻呼机THEAD:

 &LT; PagerSettings位置=​​TopAndBottom/&GT;

如果您想在页眉和页脚寻呼机,您还必须设置这些寻呼机有是 TableRowSection.TableHeader TableRowSection .TableFooter 分别。

所以使用下面的code:

  gvShipments.UseAccessibleHeader = TRUE;
gvShipments.HeaderRow.TableSection = TableRowSection.TableHeader;
如果(gvShipments.TopPagerRow!= NULL)
{
     gvShipments.TopPagerRow.TableSection = TableRowSection.TableHeader;
}
如果(gvShipments.BottomPagerRow!= NULL)
{
     gvShipments.BottomPagerRow.TableSection = TableRowSection.TableFooter;
}

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

解决方案

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;
}

这篇关于GridView控件,添加标题行中code第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆