paging gridview rebinding for each result - c#

i have a gridview with paging featuer,when i want to search item in DB and result displayed in gridview with paging
but when i jump another page gridview will Bind and list all items in DB with all pages,how can i solve this problem?
thanks in your advise
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
using (NoavaranModel.NoavaranEntities1 dbContext = new NoavaranModel.NoavaranEntities1())
{
var query = from list in dbContext.Students
select list;
lblStudentsCount.Text = query.Count().ToString();
GridView1.DataSource = query;
GridView1.DataBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();
GridView1.DataBind();
}

If I'm understanding correctly, you're calling GridView1.DataBind() at the end of your BindGridView() method, so I don't believe you need to do it again after your call for BindGridView(); in the PageIndexChanging event:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();
}
If this didn't solve it, please let me know what happens in the comments and I'll take another look at it.
Also, if you're saying that your GridView is filling up with all items from the database (when you're expecting only certain items), that indicates a problem with your query. I would try setting a breakpoint and step through the query to see what is coming back, just to make sure you're getting back your expected results.

Related

GridView don't Show on GridView_PageIndexChanging

I have a panel ViewStock where i am viewing stock in a gridview from database and DataBind() it via code. Allowed paging and created and event "OnPageIndexChanging" in gridview tag in html, Implemented the defined code above and paging in an event as follows:
HTML:
<asp:Panel ID="Panel_StockView" runat="server">
<asp:GridView ID="GridView_Stock" AllowPaging="true" OnPageIndexChanging="GridView_PageIndexChanging" runat="server"></asp:GridView>
</asp:Panel>
Code C#:
protected void LinkButton_Panel_ViewStock_Click(object sender, EventArgs e)
{
using(SqlConnection con = new SqlConnection(cs))
{
//Sql command here
/sql adapter and filled datatable
sdaStockView.Fill(dtStockView);
GridView_Stock.DataSource = dtStockView;
GridView_Stock.DataBind();
}
}
And now the Implemented Paging
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView_Stock.DataBind();
GridView_Stock.PageIndex = e.NewPageIndex;
}
it does work but partially. It does the paging and does the data correctly. But, the issue is when i click the page '2' the panel blanks out just like in the picture i uploaded See this Image, then i click the link button that redirects me to the panel again and opens the page '2' of the gridview with valid data.
How to resolve this issue?
Extract the logic which binds the GridView to data into a new method.You can call it BindData() for example:
private void BindData()
{
using (SqlConnection con = new SqlConnection(cs))
{
sdastockview.fill(dtstockview);
gridview_stock.datasource = dtstockview;
gridview_stock.databind();
}
}
Call this method inside LinkButton_Panel_ViewStock_Click to populate the GridView:
protected void LinkButton_Panel_ViewStock_Click(object sender, EventArgs e)
{
this.BindData();
}
Lastly, call it again to re-populate the GridView during paging:
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView_Stock.PageIndex = e.NewPageIndex;
this.BindData();
}
Just make those three little changes and it will work. I've tried this on my side and it's working just fine.
Save your DataSet somewhere like ViewState on LinkButton_Panel_ViewStock_Click after filling DataSet like this
ViewState["ds"] = dtStockView
In PageIndexChanging write like this
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
panel_ViewStock.visible = true;
GridView_Stock.PageIndex = e.NewPageIndex;
GridView_Stock.DataSource = ViewState["ds"] as DataSet
GridView_Stock.DataBind();
}
Hope this will help you
You can try to use:
protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)

GridView is hiding after Row Deleting Event

I am using GridView Row command Event for deleting row from Gridview, I have added RowDeleting Event of gridview,
but after deleting the GridView is getting hide.
Below is my code
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack){
LoadData()//Here I am databinding the Grid
}
}
private void LoadData()
{
var data=MyClass.GetRecords();//it returns DataTable
dg.DataSource=data;
dg.DataBind();
}
protected void dg_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName =="del")
{
//Delete Records
dg.DataBind();
}
}
public void dg_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
dg.DataBind();
}
My guess is you are missing DataSource after deleting the rows.
Try this
protected void dg_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName =="del")
{
//Delete Records
LoadData();
}
}
public void dg_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
LoadData();
}
The GridView does not show any records because you do not assign a DataSource to it before calling DataBind in dg_RowDeleting.
You need to reassign the DataSource before calling DataBind anew, because it is not set again on a PostBack.
So a typical approach for the Delete part of your dg_RowCommand method would be:
Identify the row that is to be deleted. Remove the row from the database.
Reload the data from the database.
Assign the data to the DataSource property.
Bind the GridView by calling DataBind().
For steps 2-4 you will be able to call your LoadData method from dg_RowCommand. You only need to implement dg_RowDeleting if you do not implement the deletion yourself but want to do something when a row is deleted.

Pagination of GridView in ASP.NET (Won't go to page 2,3, etc.)

I have a Gridview and I want to set Pagination. So far I have:
<asp:GridView ID="grdActivity"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
PageSize="30"
OnPageIndexChanging="gridView_PageIndexChanging">
C# code on the back is:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
grActivity.DataBind();
}
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
grdActivity.DataBind();
}
I get no errors but when I press any other page (2 and on), nothing shows, just a blank page and the GridView disappears. Am I missing something? I see hundreds of pages showing this exact code to do this.
You will need to re-assign your datasource property. After the postback the grActivity.DataSource is null, and calling DataBind on it will clear the grid.
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//re-assign your DataSource
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
//Set the new page index
grdActivity.PageIndex = e.NewPageIndex;
//apply your changes
grdActivity.DataBind();
}
Some notes:
you should consider re-factoring the code that brings you the data into a single method called on Page_Load and inside the gridview_PageIndexChanging (see #ekad's answer)
if you have a lot of data you should consider paging the data directly on the database. Right now if your table has 500 rows all will be loaded into the dataset even if just a small part of it (the page size) will be displayed in the grid
Hi you please keep the code in a separate method and call it in page load event and page index changed event, something like
protected void FillGrid()
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
grActivity.DataBind();
}
and in the page index changing event
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
FillGrid();
}
and in page load in !post back event simply call the method, like
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillGrid();
}
}
You have to set the data source of grdActivity before calling grdActivity.DataBind() in gridView_PageIndexChanging method. I would also suggest creating a separate method to avoid repeating the same code in Page_Load and gridView_PageIndexChanging
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
grdActivity.DataSource = GetDataSource();
grdActivity.DataBind();
}
}
private DataTable GetDataSource()
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
return myDataSet.Tables["tblActivity"];
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
grdActivity.DataSource = GetDataSource();
grdActivity.DataBind();
}

error paging and editing in gridview

I had gridview which retrieve Products from database by sqldatasource1 ,and my manager asked me to filter this gridview by DDL to filter gridview with specfic Model ,I add some function on gridview as edit,paging .I did my code well and gridview filtred by the Model_Id which come from DDL .But when I tried to edit any product or navigate through paging I faced this error (The GridView 'GridView1' fired event PageIndexChanging which wasn't handled. )when paging ,And this for editing (The GridView 'GridView1' fired event RowEditing which wasn't handled.)
So please any one help me.
(CS)
protected void Page_Load(object sender, EventArgs e)
{
BindGridFunction();
}
private void BindGridFunction()
{
if (DDLModel.SelectedIndex < 0)
{
GridView1.DataSource = SDSModel;
GridView1.DataBind();
}
else
{
GridView1.DataSource = SDSModel2;
GridView1.DataBind();
}
}
You need to explicitly handle PageIndexChanging() and RowEditing() event through code behind.
like
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
public void GridView1_RowEditing(Object sender, GridViewEditEventArgs e)
{
//do your code here
}

gridview_paging messing up

I have an asp.net C# web app. In it I have a gridview. The gridview gets search results from a database. Sometimes there are a lot of results, and so I wanted to use paging. Here's what I tried:
protected void grdResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdResults.PageIndex = e.NewPageIndex;
grdResults.DataBind();
}
For some reason, when I click on a page number, it shows me the EmptyDataText(There are no records to display). What code would work?
Please help.
Thank you
Try assigning the datasource in NeedDataSource event.
Cheers.
You need to reassign your datasource to grdResults before the call to DataBind().
Try this code It will absoloutly work :
protected void GV_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GV.PageIndex = e.NewPageIndex;
BindGrid();
}
public override void BindGrid()
{
query = new CommonQueries();
GV.DataSource = query.getAllBooks();
GV.DataBind();
}
the problem with your code is that you did'nt reassign the data source to your gridview !

Categories