Reputation: 1989
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.
Upvotes: 1
Views: 130
Reputation: 6586
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);
}
Upvotes: 3