locked
Gridview paging RRS feed

  • Question

  • User-1901014284 posted

    Hi,

    I have a gridview which i have setup to allow paging but when clicking on page 2 no records are displayed the gridview completely disappears even though there are enough records within the database to be displayed. please see below my code.

    HTML:

    <asp:GridView ID="ExpenseGridView" runat="server" BackColor="White"
    BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
    GridLines="Vertical" Height="168px" Width="667px" AllowSorting="True" AutoGenerateColumns="False" ForeColor="Black" CssClass="auto-style7"
    PageSize="10" AllowPaging ="True" OnPageIndexChanging="EXPGridView_PageIndexChanging">

    <AlternatingRowStyle BackColor="#CCCCCC" />
    <Columns>
    <asp:TemplateField InsertVisible="False" SortExpression="ID"><ItemTemplate>
    <a id="A1Expenses" runat="server" href='<%# "EditExpense.aspx?id=" + Eval("id")%>'>View</a>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Created_By" HeaderText="Created By" SortExpression="Created_By" />
    <asp:BoundField DataField="Expense_Date" HeaderText="Expense Date" SortExpression="Expense_Date" />
    <asp:BoundField DataField="Justification" HeaderText="Justification" SortExpression="Justification" />
    <asp:BoundField DataField="Work_Start_Time" HeaderText="Work Start Time" SortExpression="Work_Start_Time" />
    <asp:BoundField DataField="Work_End_Time" HeaderText="Work End Time" SortExpression="Work_End_Time" />
    <asp:BoundField DataField="Work_Time_DurationHours" HeaderText="Work Duration" SortExpression="Work_Time_DurationHours" />
    <asp:BoundField DataField="Visit_Call_Other" HeaderText="Visit Call Other" SortExpression="Visit_Call_Other" />

    </Columns>
    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
    </asp:GridView>
    </div>

    C#:

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

    As mentioned there is no error while running this but only the first page is displayed, when clicking on a page number at the bottom of the gridview the page refreshes but once loaded the gridview does not appear..

    Any help would be greatly appreciated.

    Many Thanks

    Jonny

    Tuesday, July 18, 2017 4:15 PM

Answers

  • User2103319870 posted

    Unfortunately I cannot find anything wrong in your code. Have your checked if dt has values during paging event. if not put a breakpoint and check if datatable dt has values in it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 21, 2017 1:31 PM

All replies

  • User2103319870 posted

    jonnygareth30

    ExpenseGridView.PageIndex = e.NewPageIndex;
    ExpenseGridView.DataBind();

    You also need to set the datasource before calling databind for gridview

    Sample COde

     <asp:GridView ID="ExpenseGridView" runat="server" DataKeyNames="Name" AutoGenerateColumns="false" AllowPaging="True" OnPageIndexChanging="GridView2_PageIndexChanging" PageSize="1">
                    <Columns>
                        <asp:BoundField DataField="Name" HeaderText="Name" />
                    </Columns>
                </asp:GridView>

    C#:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
    
            public void BindData()
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("Name");
                dt.Rows.Add("Hello1");
                dt.Rows.Add("Hello2");
                dt.Rows.Add("Hello3");
                dt.Rows.Add("Hello4");
                dt.Rows.Add("Hello1");
                dt.Rows.Add("Hello2");
                dt.Rows.Add("Hello3");
                dt.Rows.Add("Hello4");
                ExpenseGridView.DataSource = dt;
                ExpenseGridView.DataBind();
            }
    
            protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                ExpenseGridView.PageIndex = e.NewPageIndex;
                //Calling the method which will set datasource and then bind the gridview with data
                BindData();
            }

    Tuesday, July 18, 2017 8:25 PM
  • User-1901014284 posted

    Hi, thank you for your response. I have implemented the above and all looks to be working fine, when I try and navigate to the second page again the page refreshes and the datagrid disappears once the page has re-loaded.

    Wednesday, July 19, 2017 4:07 PM
  • User-271186128 posted

    Hi Jonny,

    I have implemented the above and all looks to be working fine, when I try and navigate to the second page again the page refreshes and the datagrid disappears once the page has re-loaded.

    Please set a break point and step by step to debug your code, then make sure you have re-set the GridView DataSource and bind the GridView.

    If still not working please post the relevant code or share your application, it might be easier for us to help you.

    Best regards,
    Dillion

    Friday, July 21, 2017 5:58 AM
  • User-1901014284 posted

    Hi A2H,

    I am reviewing the code you have provided above again and would like to clarify when you have dt.Rows.Add("Hello"); how do i select the data from my db table to populate these rows? I have tried using ID but when running my code the gridview displays but with no records being displayed. I have also set the DataKeyNames="ID"

    Any help would be greatly appreciated.

    Many Thanks

    Jonny

    Friday, July 21, 2017 9:56 AM
  • User-1901014284 posted

    Thank you for getting back to me, my original code is as below, having implemented A2H advice the same problem has occurred but there is no error message. Having debugged the code there are no errors within the C# code (please see below) displayed while debugging, nothing stands out while debugging. 

    HTML:

    <asp:GridView ID="ExpenseGridView" runat="server" BackColor="White" 
    BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 
    GridLines="Vertical" Height="168px" Width="667px" AllowSorting="True" AutoGenerateColumns="False" ForeColor="Black" CssClass="auto-style7"
    PageSize="10" AllowPaging ="True" OnPageIndexChanging="EXPGridView_PageIndexChanging">

    <AlternatingRowStyle BackColor="#CCCCCC" />
    <Columns>
    <asp:TemplateField InsertVisible="False" SortExpression="ID"><ItemTemplate>
    <a id="A1Expenses" runat="server" href='<%# "EditExpense.aspx?id=" + Eval("id")%>'>View</a> 
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Created_By" HeaderText="Created By" SortExpression="Created_By" />
    <asp:BoundField DataField="Expense_Date" HeaderText="Expense Date" SortExpression="Expense_Date" />
    <asp:BoundField DataField="Justification" HeaderText="Justification" SortExpression="Justification" />
    <asp:BoundField DataField="Work_Start_Time" HeaderText="Work Start Time" SortExpression="Work_Start_Time" />
    <asp:BoundField DataField="Work_End_Time" HeaderText="Work End Time" SortExpression="Work_End_Time" />
    <asp:BoundField DataField="Work_Time_DurationHours" HeaderText="Work Duration" SortExpression="Work_Time_DurationHours" />
    <asp:BoundField DataField="Visit_Call_Other" HeaderText="Visit Call Other" SortExpression="Visit_Call_Other" />

    </Columns>
    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
    </asp:GridView>
    </div>

    C#:

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

    Friday, July 21, 2017 10:10 AM
  • User2103319870 posted

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

    Unfortunately you still didn't set the datasource for your Gridview on page load. Please set that and retry operation.

    Friday, July 21, 2017 12:49 PM
  • User-1901014284 posted

    Sorry, I copied my old code, please see below updated code:

    protected void EXPGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
    ExpenseGridView.PageIndex = e.NewPageIndex;
    ExpenseGridView.DataSource = dt;
    ExpenseGridView.DataBind();

    Friday, July 21, 2017 12:59 PM
  • User-1901014284 posted

    I have also added the below code under my query:

    ExpenseGridView.AllowPaging = true;

    please see below my query code in full:

    using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
    {
    var query = @"SELECT eef.Expense_Date,
    vco.Visit_Call_Other,
    eef.Justification,
    eef.Work_Start_Time,
    eef.Work_End_Time,
    eef.Work_Time_DurationHours,
    eef.Created_By,
    eef.Client_ID,
    EEF.id
    FROM " +
    "Expense_Entry_Form eef " +
    "LEFT JOIN Visit_Call_Other vco ON (eef.Visit_Call_Other = vco.ID) " +
    "WHERE " +
    "(Client_ID = @ClientId AND Created_By = @userName)" +
    "OR (Client_ID = @ClientId AND jobRoleID = @jobRole)" +
    "OR (Client_ID = @ClientId AND Admin_Team = @adminTeam)" +
    "ORDER BY Expense_Date DESC";

    cmd = new SqlCommand(query, connection);
    da = new SqlDataAdapter(cmd);

    cmd.Parameters.AddWithValue("@userName", Session["userName"]);
    cmd.Parameters.AddWithValue("@jobRole", Session["jobRole"]);
    cmd.Parameters.AddWithValue("@adminTeam", Session["Admin_Team"]);
    cmd.Parameters.AddWithValue("@ClientId", Request.QueryString["id"]);
    dt.Clear();
    da.Fill(dt);

    if (dt.Rows.Count > 0)
    {
    ExpenseGridView.DataSource = dt;
    ExpenseGridView.AllowPaging = true;
    ExpenseGridView.DataBind();
    }

    connection.Close();

    Friday, July 21, 2017 1:01 PM
  • User2103319870 posted

    Unfortunately I cannot find anything wrong in your code. Have your checked if dt has values during paging event. if not put a breakpoint and check if datatable dt has values in it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 21, 2017 1:31 PM
  • User-1901014284 posted

    Thanks for all your help. As I could not get this to work with my original code i tried another way and it works fine:

    string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
    using (SqlCommand command = new SqlCommand("SELECT eef.Client_ID, eef.ID, eef.Expense_Date, eef.Created_By, eef.Justification, eef.Work_Start_Time, eef.Work_End_Time, eef.Work_Time_DurationHours, vco.Visit_Call_Other " +
    "FROM Expense_Entry_Form eef " +
    "LEFT JOIN Visit_Call_Other vco ON (eef.Visit_Call_Other = vco.ID) " +
    "WHERE Client_ID = @ClientId"))
    {
    using (SqlDataAdapter sdaexp = new SqlDataAdapter())
    {
    command.Connection = con;
    command.Parameters.AddWithValue("@ClientId", Request.QueryString["ID"]);
    sdaexp.SelectCommand = command;
    using (DataTable dt = new DataTable())
    {
    sdaexp.Fill(dt);
    ExpenseGridView.DataSource = dt;
    ExpenseGridView.DataBind();
    }

    I am just thinking is there a way I could add parameters to the above code?

    Monday, July 24, 2017 3:29 PM
  • User2103319870 posted

    command.Parameters.AddWithValue("@ClientId", Request.QueryString["ID"]);

    You already have a parameter added in your code. Are you facing any issue while adding new parameters

    Monday, July 24, 2017 3:35 PM