Unanswered GridView Paging

  • Thursday, July 27, 2006 11:17 AM
     
     

    Hi!

    I wrote a store procedure to display all data of a table in database. I used a function (its name is GetAllCustomer in class CustomerDB) to read above store that return value is a SqlDataReader. When I use GridView (name=grvCustomer) to display data with code:

    grvCustomer.DataSource = CustomerDB.GetAllCustomer();

    grvCustomer.DataBind();

    and I want to paging it, I set Allow Paging is true, but not success.

    Help me!

     

All Replies

  • Friday, July 28, 2006 8:48 AM
     
     

    Hi,

    SqlDataReader does not support server side data paging, so i advice you to use DataTable, much safe and easy to use

    Here there is an example i wrote it quickly in VS2005, i hope it helps

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
             bind();
        }
    }

    private
    DataTable getTable()
    {
         SqlConnection conn = new SqlConnection("Data source=localhost;user id=user;password=pass;database=ClassProject");
         SqlDataAdapter adap = new SqlDataAdapter("Select * from lessons", conn);
         DataTable dt = new DataTable();
         adap.Fill(dt);
         return dt;
    }

    private void bind()
    {
         DataTable dr = getTable();
         GridView1.DataSource = dr;
         GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
         GridView1.PageIndex = e.NewPageIndex;
         bind();
    }

    First you should set the AllowPaging true at the properties window or in page_Load

    Best Regards