问题
I've gone through several articles and tutorials, but I just can't figure this out. Everything basically says, "oh just turn on AllowPaging, and you're done!" When I do that, yes, I can see the paging controls under the GridView in the Design View, but when I compile, I can't see the page numbers in the running site.
One thing I noticed different from all of the examples, is that I do the data work from the code-behind. Therefore my GridView is simply:
<asp:GridView ID="gvlatest" runat="server" Width="99%" AllowSorting="True"
onrowdatabound="gvlatest_RowDataBound" onsorting="gvlatest_Sorting"
AllowPaging="True" PageSize="2" />
What I mean by doing the data work from behind, is that all the columns and everything, are constructed from code into a DataTable, and then I set the GridView's DataSource to the DataTable. For example, a grossly abbreviated version of what I have:
DataTable temptable = new DataTable();
DataColumn titlecol = new DataColumn();
titlecol.ColumnName = "Title";
temptable.Columns.Add(titlecol);
gvlatest.DataSource = temptable;
gvlatest.DataBind();
This is just a personal preference I guess, and to be honest I've actually never learned how to use the DataSource controls and such that all the examples are using, where you build the GridView in the .aspx file with the columns, data source, etc. So I'm guessing that my problem lies in that general direction...
The question is, what am I doing wrong? Why are the page numbers not showing up? Is setting "AllowPaging" to true really all that I need to do?
回答1:
For Paging to work, your datasource must support it. If it does not, like a DataTable, then you have to do this yourself.
This code should help.
OnPageIndexChanging="myGridview_PageIndexChanging"
protected void myGridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gv = (GridView)sender;
DataView dv = gv.DataSource as DataView;
DataTable dataTable = dv.Table;
gv.DataSource = myDataTable;
gv.PageIndex = e.NewPageIndex;
gv.DataBind();
}
回答2:
you have to use the page_index changing event in the gridview to implement paging in the gridview refer this link:
http://forums.asp.net/t/1245611.aspx
hope it helps
回答3:
You can disable particular column and add Paging
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
//----------------------------------Grid view column invisible------------------------------------------------------------
if (Request.QueryString.Get("show") == "all")
GridView1.Columns[0].Visible = true;
else
GridView1.Columns[0].Visible = false;
//-------------------------------------------------------------------------------------------------------------------------
}
protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
gvbind();// Grid View Binded
}
// Source Code
allowpaging="true" OnPageIndexChanging="Gridview1_PageIndexChanging" pagesize="2"
来源:https://stackoverflow.com/questions/10974701/asp-net-gridview-not-displaying-page-numbers