StudentIT
StudentIT

Reputation: 481

ASP.NET why Gridview Paging on page 2 and the rest of it are disappear?

I have so many list of ServerName in GridView, so I decide to add paging. The data show list result on page 1 but on page 2 and the rest of it is not display anything. I already have OnPageIndexChanging="GridViewServer_PageIndexChanging" in GridViewServer property. Please help! Here is c# code behind,

protected void GridViewServer_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewServer.PageIndex = e.NewPageIndex;
        GridViewServer.DataBind();
    }

A GridView binding function codes,

public void BindGridView()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);

        conn.Open();

        string sqlquery = ("SELECT * FROM tblServer");

        SqlCommand command = new SqlCommand(sqlquery, conn);

        SqlDataAdapter adp = new SqlDataAdapter(command);

        DataSet ds = new DataSet();

        adp.Fill(ds);

        GridViewServer.DataSource = ds.Tables[0];

        GridViewServer.DataBind();
    }

Upvotes: 0

Views: 5260

Answers (3)

syed mohsin
syed mohsin

Reputation: 2938

You have to give datasource to GridViewServer every time on page index changed. so the code would be like this

protected void GridViewServer_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridViewServer.PageIndex = e.NewPageIndex;
    GridViewServer.Datasource = MethodReturningDataTable();
    GridViewServer.DataBind();
}

Upvotes: 0

timbck2
timbck2

Reputation: 1066

Here's a very good tutorial (with sample code) on custom GridView paging. This makes the paging controls look like the familiar ones you see on a lot of search engines, forums, etc.

http://geekswithblogs.net/aghausman/archive/2009/05/18/custom-paging-in-grid-view.aspx

Upvotes: 0

Joel Etherton
Joel Etherton

Reputation: 37533

You need to set your GridView's datasource appropriately. You can't just call DataBind if the datasource isn't properly set. Basically what it amounts to is binding your GridView to null (which would have no page 2). I would recommend having a private method that is responsible for this process and whenever you need to bind you call that.

private void BindGridViewServer()
{
    GridViewServer.DataSource = GetYourData();  // This should get the data
    GridViewServer.DataBind();
}

Call this method from within your event with:

protected void GridViewServer_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridViewServer.PageIndex = e.NewPageIndex;
    BindGridViewServer();
}

This could be made more extensible by passing in the GridView as a parameter, but you'd have to have other parameters as well to make sure the method retrieves the proper data.

Upvotes: 2

Related Questions