How to setup paging with Gridview and code behind (no data source)

399 views Asked by At

I am looking for some help on paging using GridView and a data source that is set in my code behind. I want to set my queries to the database to only return 50 records at a time (about 20,000 in all). This all works except I can't get paging setup properly. I think default paging would work, but I don't see anywhere that I can tell GridView my max number of pages to show in the navigation or the overall number of records aside from the 50 it knows about.

I really need a blog or link to some step by step instructions on how to do this.

Here is my gridview:

<asp:GridView
    ID="gvResults"
    CellPadding="4"
    HeaderStyle-BackColor="DarkGray"
    runat="server"
    OnPageIndexChanging="gvResults_PageIndexChanging"
    AutoGenerateColumns="False" AllowPaging="True" ForeColor="#333333" ShowHeaderWhenEmpty="True" PageSize="50" Width="100%" EnableSortingAndPagingCallbacks="True">
    <PagerSettings PageButtonCount="10" Position="TopAndBottom" Mode="NumericFirstLast" />
    <AlternatingRowStyle BackColor="White" />
    <EditRowStyle BackColor="#0A5EA7" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#0A5EA7" Font-Names="" Font-Size="Medium" ForeColor="White" Font-Bold="True"></HeaderStyle>
    <PagerStyle BackColor="#0A5EA7" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    <Columns>
        <asp:HyperLinkField HeaderText="Item Number" DataTextField="Change Number" DataTextFormatString="{0}" DataNavigateUrlFormatString="./item.aspx?ID={0}" DataNavigateUrlFields="Item Number" />
        <asp:BoundField HeaderText="" DataField="Attachment(s)" HeaderImageUrl="images/paperclip1sm.png" HeaderStyle-HorizontalAlign="Center">
            <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
        </asp:BoundField>
        <asp:BoundField DataField="Part" HeaderImageUrl="images/part1sm.png" HeaderStyle-HorizontalAlign="Center">
            <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
        </asp:BoundField>
        <asp:BoundField HeaderText="Description of Change" DataField="Description" />
        <asp:BoundField HeaderText="Status" DataField="Status" />
        <asp:BoundField HeaderText="Reason Code" DataField="Reason Code" />
        <asp:BoundField HeaderText="Reason" DataField="Reason" />
    </Columns>
</asp:GridView>

Here is my query to get the total record count:

//This will get the total number of records.
    private int getMaxRecordCount()
    {
        int lastRecord = 0;
        string ecoNumber = itemInput.Value.ToUpper().Trim();
        string connStr = "I REMOVED THIS BIT, BUT THIS DOES WORK";
        OracleConnection conn = new OracleConnection(connStr);
        string sqlCountString = "select count(*) from table1 where field1 like '" + inputNumber + "%'";
        conn.Open();
        OracleCommand cmdCount = new OracleCommand(sqlCountString, conn);
        OracleDataReader countReader = cmdCount.ExecuteReader();

        while (countReader.Read())
        {
            lastRecord = Int32.Parse(countReader[0].ToString());
            itemResults.Text = "Total Records Returned: " + lastRecord + ")";
        }
        conn.Dispose();
        return lastRecord;
    }

And here is my code that gets the subset of records:

private void bindGrid(int startRecord, int endRecord)
    {
        try
        {
            otherMessage.Visible = false;
            otherMessage.ForeColor = default(Color);
            ecoGrid.Visible = false;
            gvResults.Visible = false;
            string itemNumber = ecoInput.Value.ToUpper().Trim();
            string connStr = "I REMOVED THIS BIT, BUT THIS DOES WORK";
            OracleConnection conn = new OracleConnection(connStr);
            string sqlString = @"
                                I REMOVED THIS BIT, BUT THIS DOES WORK
                                ";
            sqlString = sqlString + itemNumber + "%' AND ROWNUM BETWEEN " + startRecord + " AND " + endRecord;

            conn.Open();
            OracleCommand cmd = new OracleCommand(sqlString, conn);
            OracleDataReader Reader = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[7] {
                    new DataColumn("Item Number"),
                    new DataColumn("Attachment(s)"),
                    new DataColumn("Part"),
                    new DataColumn("Status"),
                    new DataColumn("Reason Code"),
                    new DataColumn("Reason"),
                    new DataColumn("Description")

                });

            while (Reader.Read())
            {
                DataRow dr = null;
                dr = dt.NewRow();
                dr["Item Number"] = Reader[0].ToString();
                string att = hasAttachments(Reader[0].ToString());
                dr["Attachment(s)"] = att;
                dr["Part"] = "";
                dr["Status"] = Reader[1].ToString();
                dr["Reason Code"] = Reader[2].ToString();
                dr["Reason"] = Reader[3].ToString();
                dr["Description"] = Reader[4].ToString();

                dt.Rows.Add(dr);
            }

            conn.Dispose();

            if (dt.Rows.Count > 0)
            {
                itemGrid.Visible = true;
                gvResults.Visible = true;
                gvResults.DataSource = dt;
                gvResults.DataBind();
            }
            else
            {
                itemGrid.Visible = false;

                otherMessage.Visible = true;
                otherMessage.Text = "No Results Found.";
            }
        }
        catch
        {
            itemGrid.Visible = false;
            otherMessage.Visible = true;
            otherMessage.ForeColor = Color.Red;
            otherMessage.Text = "There was an error processing your request. Please contact the IT Helpdesk for additional assistance.";
        }
    }
0

There are 0 answers