C# 如何使用Gridview和代码隐藏设置分页(无数据源)

C# 如何使用Gridview和代码隐藏设置分页(无数据源),c#,asp.net,gridview,custompaging,C#,Asp.net,Gridview,Custompaging,我正在寻找一些关于使用GridView和在我的代码隐藏中设置的数据源进行分页的帮助。我想将对数据库的查询设置为一次只返回50条记录(总共大约20000条)。这一切工作,除了我不能得到正确的分页设置。我认为默认分页可以工作,但我看不到任何地方可以告诉GridView我在导航中显示的最大页数,或者除了它知道的50条记录之外的总记录数 我真的需要一个博客或链接到一些一步一步的指导如何做到这一点 以下是我的gridview: 下面是我获取记录子集的代码: <asp:GridView ID=

我正在寻找一些关于使用
GridView
和在我的代码隐藏中设置的数据源进行分页的帮助。我想将对数据库的查询设置为一次只返回50条记录(总共大约20000条)。这一切工作,除了我不能得到正确的分页设置。我认为默认分页可以工作,但我看不到任何地方可以告诉
GridView
我在导航中显示的最大页数,或者除了它知道的50条记录之外的总记录数

我真的需要一个博客或链接到一些一步一步的指导如何做到这一点

以下是我的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>
//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;
    }
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.";
        }
    }