1

I have a gridView:-

<asp:GridView ID="gvw_Lab_Details" AllowPaging="true" PageSize="3" OnPageIndexChanging="gvw_Lab_Details_PageIndexChanging" runat="server" EmptyDataText="No Previous Enrollments were Found." SkinID="gridviewSkin2">
</asp:GridView>

On the server side:-

    public DataSet LabDS
    {
        get { return (DataSet)ViewState["LabDetails"]; }
        set { ViewState["LabDetails"] = value; }
    }

    #region Initialize.
    string _Requestdate = string.Empty;
    string _ID = string.Empty;
    #endregion

    #region Page Methods
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // extracting the Lab_Requested Date from Query String.
            _Requestdate = (Request.QueryString["info"]);
            _ID = ((MyStateBag)Session["MyStateBag"]).MemberID;
            GetLabResults();
        }
    }

    #endregion

    private void GetLabResults()
    {
      using (SqlConnection cn = new SqlConnection(DBConnect.SqlServerConnection))
       {
            cn.Open();
            #region Pulls Existing Enrollments from SQL
            DataTable dt = new DataTable();
            using (SqlCommand cm = cn.CreateCommand())
            {
                // Create SQL Statement
                StringBuilder ct = new StringBuilder();
                ct.AppendLine("SELECT DISTINCT Key, Date, "
                + "NAME_First + ' ' + NAME_MI + ' ' + NAME_Last, "
                RESULT_VALUE, RESULT_UNITS ");
                ct.AppendLine("FROM [test].[dbo].[test]");
                ct.AppendLine("WHERE Date = @Date and ID = @ID");
                cm.Parameters.AddWithValue("@Date", _Requestdate);
                cm.Parameters.AddWithValue("@ID", _ID);
                cm.CommandType = CommandType.Text;
                cm.CommandText = ct.ToString();
                // Execute 
                cm.ExecuteNonQuery();

            #region Populate Gridview with extracted Data.
                SqlDataAdapter dr = new SqlDataAdapter(cm);
                dr.Fill(dt);
                this.LabDS  = new DataSet();
                this.LabDS.Tables.Add(dt);
                LoadGridView(1);
             #endregion
            }
            #endregion

        private void LoadGridView(int PageIndex)
        {
            gvw_Lab_Details.DataSource = this.LabDS;
            gvw_Lab_Details.DataBind();
        }


        protected void gvw_Lab_Details_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            LoadGridView(e.NewPageIndex);
        }

However my indexing doesn't seem to be working aka results of 3 or less are shown on the first page but once there are more than 4 results I get my default "No previous enrollments were found". Any insights on where I am going wrong?

this a .NET web application.

Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
Philo
  • 1,931
  • 12
  • 39
  • 77

1 Answers1

3

You're missing one important piece of information. You have to tell the GridView which page you are changing to:

 protected void gvw_Lab_Details_PageIndexChanging(object sender, GridViewPageEventArgs e)
 {
        gvw_Lab_Details.PageIndex = e.NewPageIndex;
        LoadGridView(e.NewPageIndex);
 }
Rick S
  • 6,476
  • 5
  • 29
  • 43
  • I just noticed that although the solution works, the first 6 records are omitted. So if my SQL management studio responds with 33 rows, the .NET gridiew shows only 27. and if the SQL MS responds with 6, my .NET gridview shows 0 records. Any idea why? – Philo Mar 07 '14 at 21:00
  • Page indexing is zero based. I bet your thinking 1 is the first page. Zero is the first page. – Rick S Mar 07 '14 at 21:03
  • LoadGridView(1); I tried making that (0) ... both returns the same results. Which is why I was confused and it still wouldn't make sense because My Gridview has a max of 3 records per page. If I were to loose 1 page then I would only be short by 3 records. Whereas in reality I am short by 6 records, that is 2 pages. – Philo Mar 07 '14 at 21:10