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?
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();
}
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
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"
Related
I have a GridView control that uses a datatable for a data source. I have enabled paging on the control, and the GridView shows the number of rows I've specified as the PageSize. However, there is no paging navigation, so I have no way to change the page.
Question: How do I get the paging navigation to show?
GridView definition:
<asp:GridView runat="server" ID="gvResults" CssClass="report" DataKeyNames="LogId" AllowSorting="True" AllowPaging="True" PageSize="5" OnRowDataBound="gvResults_OnRowDataBound" OnPageIndexChanging="gvResults_OnPageIndexChanging"></asp:GridView>
C#:
//...stuff that gets data from database
DataTable dt = oDs.DataSet.Tables[0];
gvResults.DataSource = dt;
gvResults.DataBind();
NOTE: I've verified in debugger that the datatable dt has more than 100 rows
And the OnPageIndexChanging() event, based on the answer to this question, although it didn't work in my case (I'm not sure why it's necessary since the event shouldn't trigger until you attempt to go to the next page, which would require the paging navigation to show in the first place, right?):
protected void gvResults_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gv = (GridView)sender;
DataView dv = gv.DataSource as DataView;
if (dv != null)
{
DataTable dt = dv.Table;
gv.DataSource = dt;
}
gv.PageIndex = e.NewPageIndex;
gv.DataBind();
}
Here is what I see:
I expect to see some way to page through the results.
I have also tried adding a PagerSettings to my GridView, like so:
<asp:GridView runat="server" ID="gvResults" CssClass="report" DataKeyNames="LogId" AllowSorting="True" AllowPaging="True" PageSize="5" OnRowDataBound="gvResults_OnRowDataBound" OnPageIndexChanging="gvResults_OnPageIndexChanging">
<PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" LastPageText="Last" NextPageText="Next" PreviousPageText="Previous" Position="Bottom"></PagerSettings>
</asp:GridView>
Here is what the rendered HTML looks like for that last row. I modified the css during runtime to expand the row so it's clear that the row is empty. There are no paging controls being hidden by css.
After much banging of head and gnashing of teeth, I finally figured this out.
I have another event that operates OnRowDataBound() which formats a particular column to HTML, and this conversion resulted in no paging controls when it operated on the footer row.
The solution was to check that the bound row is a DataRow before doing the conversion.
protected void gvResults_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (!(sender is GridView) || e.Row.RowType != DataControlRowType.DataRow) return;
//do stuff
}
And then happiness ensued:
How do I add paging to this GridView?
C# code:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
Error
Specified argument was out of the range of valid values. Parameter name: index
You again you need to in page index changing event. Here you have not written that.
See the link
myGridView.DataSource = your datasource here;
myGridView.PageIndex = e.NewPageIndex;
myGridView.DataBind();
I think what I do is I write the page index code and then call the actrual bindmethod of the grid after that:
myGridView.PageIndex = e.NewPageIndex;
BindmyGridView();
where
private void BindmyGridView()
{
myGridView.DataSource = lst; //where lst is the datasource
myGridView.DataBind();
}
Also have you gone through these links:
Link 1
Link 2
Allow paging is very simple.Call PageIndexChanging event of grid view like
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = Your datasource here;
GridView1.DataBind();
}
Hope it works for you..
You need to use a pageable data source. The simplest way is to just use an SqlDataSource.
Also, note that PageIndex is zero-based.
If you're using a List or a Collection etc., it needs to be big enough, which is a nice waste of space. Basically, if you're doing manual paging, and you're using a List, you're going to have to create the List as big as the full record count of your result, and only fill in the proper page. Not very practical, and quite wasty, but it's simple enough.
Apart from creating your own pageable data source, there's also another, nicer way. You can use the AllowCustomPaging property to specify that you're already passing paged data. Just set VirtualItemCount to the record count, and pass just the one page in the DataSource and call DataBind as usual. That should do :)
I have a gridview, and I have a SelectedIndexChanged event on it...
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow Row = GridView1.SelectedRow;
//do some stuff
}
Then I get an error...
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
I do not understand why, the Gridview is being binded in pageload. but not in post back...
if (!IsPostBack)
{
GridView1.DataSource = UserAccounts;
GridView1.DataBind();
}
The asp.net DataSource controls handle this for you automatically but if you are manually binding your GridView, you will need to also bind it on PostBack. If you make changes to the data source based on filters, etc., you will need to rebind it.
The first thing: When post back to server, your GridView1 will re-initialize so that GridView1.DataSource will lost the previous data
if (!IsPostBack)
{
GridView1.DataSource = UserAccounts;
GridView1.DataBind();
}
The second thing: if you manually bind your GridView with your custom DataTable, List .... you must implement RowCommand with specific DataKey.
Please take a look at this article http://aspspirits.blogspot.com/2012/08/how-to-get-rowindex-of-aspnet-gridview.html
I have an ASP.NET web app (C#) where I get some information from a datasource and display it in the the gridview. I wanted to enable paging, but for some reason, paging doesn't work. I researched a little online, and I found that paging is done differently if a dataset is used. When I click on a page number, it refreshes, and says that there are no records to display. I call this function in the click function of a button:
bindGrid(cmd);
Here's my bind method:
private void bindGrid(OracleCommand comm)
{
OracleDataAdapter adapter = new OracleDataAdapter(comm);
DataSet ds = new DataSet();
ds.Tables.Add("Results");
adapter.Fill(ds.Tables["Results"]);
grd.DataSource = ds;
grd.DataBind();
}
Paging Method:
protected void grdResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grd.PageIndex = e.NewPageIndex;
grd.DataBind();
}
How am I supposed to do paging with a dataset? Can someone help please?
You also need to fetch the data :)
So instead of this:
protected void grdResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grd.PageIndex = e.NewPageIndex;
grd.DataBind();
}
you should use:
protected void grdResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grd.PageIndex = e.NewPageIndex;
//Create command
bindGrid(comm);
}
Instead of grd.DataBind() in your paging method call bindGrid(). Or better use some built-in 'business objects' for data binding like ObjectDataSource
The simplest way to do paging is to set AllowPaging="yes" in your GridView, and don't do anything in the code-behind. The GridView will page through its dataset on its own. Do NOT rebind.
That's fine for small datasets, but not so good for a phone book. As Tadas said, ObjectDataSource is the way to go for handling paging yourself, on server side. With ObjectDataSource, you won't DataBind at all; the controls handle that. However, you do need to supply a Select method (usually static) on an object, and it needs to have parameters for first row and page size (set these as attributes on the ObjectDataSource). You will allso need to implement a Count method to return the full dataset size; otherwise paging won't work. Using an ObjectDataSource this way, you shift the burding of paging to your data layer, and the page will load much faster.
I'm new to ASP.NET.
Im developing ASP.NET C# web form which creating GridView components dynamically and filling them using the data received from my webservice.
I'm creating these GridView components programmatically in server-side (cs file)- it has to be flexible - 1 GridView and sometimes 10 GridView components.
The problem occurs when I'm trying to add pagination - whenever the user clicks the "next" page, the whole page is getting refreshed because of a postBack and I'm loosing all my data and the page returns Blank/Empty.
I used PlaceHolder to hold the GridView components, while searching for a solution, I found UpdatePanel as a better alternative - as far as I understand the page can be partially refreshed - which means that only the UpdatePanel has to be refreshed...but it doesn't work.
The following code sample is my TEST, the UpdatePanel is the only component initiated in the client side (.aspx page), the rest initiated programmatically in .cs .
how can I solve the issue described above?
Why does the whole page is getting refreshed and I'm loosing my data?
can you recommend another way? can provide me with any code sample?
If I'm not rebuilding the GridView, it doesn't work...
Here is my Default.aspx.cs
public partial class TestAjaxForm : System.Web.UI.Page
{
DataTable table;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
bindGridView();
}
public void bindGridView()
{
GridView gridView1 = new GridView();
gridView1.AutoGenerateColumns = true;
gridView1.PageSize = 2;
gridView1.AllowPaging = true;
gridView1.PagerSettings.Mode = PagerButtons.Numeric;
gridView1.PagerSettings.Position = PagerPosition.Bottom;
gridView1.PagerSettings.PageButtonCount = 10;
gridView1.PageIndexChanging += new GridViewPageEventHandler(this.GridView1_PageIndexChanging);
table = new DataTable();
table.Columns.Add("FirstName");
table.Columns.Add("LastName");
DataRow row = table.NewRow();
row["FirstName"] = "John";
row["LastName"] = "Johnoson";
table.Rows.Add(row);
row = table.NewRow();
row["FirstName"] = "Johnny";
row["LastName"] = "Marley";
table.Rows.Add(row);
row = table.NewRow();
row["FirstName"] = "Kate";
row["LastName"] = "Li";
table.Rows.Add(row);
panel.ContentTemplateContainer.Controls.Add(gridView1);
gridView1.DataSource = table;
gridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gridView1 = (GridView)sender;
gridView1.PageIndex = e.NewPageIndex;
gridView1.DataSource = table;
gridView1.DataBind();
}
}
Thank you.
If you want a custom GridView approach to work, you have to recreate and rebind the grid on every page load... that's the problem with dynamic Grids... Dynamic controls don't retain their viewstate, but if you added the grid and dynamically generated the columns, that would work easier for you (because I think it may dynamically remember the columns, or you can set AutoGenerateColumns to true, and it brings in your data row column names).
HTH
You should create your GridView and add it to the control tree in the PreInit or Init event handler so that they have a chance to properly bind to ViewState, and it should be done whether or not there's a postback: if you don't add them in a postback, they obviously won't be there to display anything.
DataBinding can happen later in the page life cycle.
Paging only seems to work if you also use an ObjectDataSource; I've never been able to get it to work with a GridView by itself.
I think the easiest thing to do is have the gridview declared in the aspx page inside the place holder, while the place holder is also contained inside the update panel.
For what I can see, John is not doing anything that can't be declared in the markup itself so that shouldn't be a problem. If for some reason the gridview should not be displayed on the screen, it suffices to set the Visible property of the placeholder to false.
As far as paging is concerned, as RickNz correctly points out, it will only retain the state if you bind the gridview to either a LinqDatasource, SqlDatasource or ObjectDatasource but if you bind it to custom business objects, what you need to do is save the data in Session and rebind it again on PageIndexChanged.
In pseudo code, would be sometging like this.
Page_load
{
If (!IsPostBack)
Binddata();
}
private void Binddata()
{
var data = Getdata();
Gridview1.DataSource= data;
Gridview.DataBind();
Session["data"]=data; // cache the data
}
protected void Gridview1_indexchanged(object sender, GridviewPageEventArgs e)
{
var data= Session["data"];
Gridview1. DataSource=data;
Gridview1.DataBind();
GridView1.PageIndex=e.NewPageIndex;
}