paging an unbound gridview - c#

can anyone point me to any good examples of how to page an unbound gridview?
Im binding it to a LINQ search query generated on a buttons click event.
thanks

got it working using
protected void grid_PageIndexChanging1(object sender, GridViewPageEventArgs e)
{ grid.PageIndex = e.NewPageIndex;
grid.DataBind();
}

Related

How to add paging to GridView?

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 :)

changing data type of autogenerated column

I have a Gridview that auto generates columns...
The problem is, the data type are all imageurl.
when the gridview populates, the url is in label format.
I could define them as templatefield which works, but the requirement is to auto generate.
I read on MSDN of this, but i dont know how to proceed from there
private void BUChecker_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
}
You can alter this on its TemplateFields. See GridView controls'.
see this for your reference.
I think you use RowDataBound as follows In case you don't want to use template field
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//find your cell suppose it is Cell 0
string imgUrl=e.Row.Cells[0].Text;
Image img = new Imge();
img.ImageUrl =imgUrl;
e.Row.Cells[0].Controls.Add(img);
}
}

ASP.Net GridView not displaying page numbers

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"

Parameterised Drill down with Gridview

I'm trying to get a Gridview "select" link to drill down to a specific page for the selected row in ASP.NET with C#. This gridview is generated dynamically in the page_load, and is databound from a fairly simple SQL select query. Each row has a unique id, and I would like this to be passed as a parameter in the url when the select button is clicked for that row. So, when you click the "select" button on the row with an id value of 9 (note - not the id as defined by the gridview, but the one from the SQL query) you are redirected to an address such as moreDetail.aspx?id=9.
However, when trying to pass the id into the event handler I've hit issues... GridView.SelectedIndexChanging takes the usual (object sender, EventArgs e) as parameters and nothing else, and since the Gridview is created at Page_Load the EventArgs class is useless. I can't seem to find any way to pass the id that I have retrieved earlier into the event handler.
After lots of searching,I tried creating a class that extends EventArgs (obviously with my extra parameter added in), but it seems using any parameters other than (object sender, EventArgs e) just won't work. I could theoretically redo the SQL query within the event handler, but that seems to me a terrible way to achieve what I'm looking for, so I'm hoping someone will be able to see what I've got wrong here, because I'm sure I'm missing something obvious.
Some code - grid.SelectedRow.Cells[0] will contain the parameter I want to pass:
In Page_Load:
GridView grid = new GridView();
grid.DataSource = source;
CommandField selectField = new CommandField();
selectField.ShowSelectButton = true;
selectField.SelectText = "View Jobs";
grid.Columns.Add(selectField);
grid.SelectedIndexChanging += grid_SelectedIndexChanging;
grid.DataBind();
content.Controls.Add(grid);
And the Event Handler:
protected void grid_SelectedIndexChanging(object sender, EventArgs e)
{
Response.Redirect("ViewCustomer.aspx?id=" + grid.SelectedRow.Cells[0]);
}
Obviously this doesn't work because the scope of grid doesn't extend to the handler...but how can I get access to that data?
You need to cast sender to GridView to reference your calling GridView:
protected void grid_SelectedIndexChanging(object sender, EventArgs e)
{
GridView grid = (GridView)sender;
Response.Redirect("ViewCustomer.aspx?id=" + grid.SelectedRow.Cells[0]);
}

gridview paging using dataset

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.

Categories

Resources