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

时间:2017-09-11 21:11:20

标签: c# asp.net gridview custompaging

我正在寻找一些使用GridView进行分页的帮助以及我后面的代码中设置的数据源。我想将查询设置为数据库,一次只返回50条记录(总共约20,000条)。除了我无法正确进行分页设置外,这一切都有效。我认为默认分页可以正常工作,但我不会在任何地方看到GridView我在导航中显示的最大页数或者除了它知道的50之外的总记录数。< / p>

我真的需要一个博客或链接到一些有关如何执行此操作的分步说明。

这是我的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.";
        }
    }

0 个答案:

没有答案