我正在寻找一些关于分页使用的帮助GridView
以及在我的代码中设置的数据源。我想将我的查询设置为一次只返回 50 条记录(总共大约 20,000 条)。这一切都有效,除了我无法正确设置分页。我认为默认分页会起作用,但是除了它知道的 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.";
}
}