【发布时间】:2013-11-26 04:56:05
【问题描述】:
我是 C# 新手,使用 Visual Studio 2010。
我创建了一个网格视图并启用了分页。它适用于所有数据。
但我在页面顶部的下拉菜单和过滤器按钮很少。当我按下每个过滤器按钮时,网格视图仅显示过滤后的数据。
我为按钮单击事件编写了每个 SQL 语句。
假设我有 60 条记录,并且网格视图显示每页 10 条记录。当我单击过滤器按钮并假设它仅显示 25 条记录时。这意味着只有 3 页显示。没关系并且有效。但是当我点击第二页或第三页时,它会再次显示所有数据,这意味着 5 页。
我知道问题在于它运行默认 SQL 查询(我用它以图形方式将数据绑定到网格视图)。但我需要知道如何解决它。我只需要在不更改过滤数据的情况下显示每个页面。
这是我的网格视图代码:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="DocumentID"
DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333" GridLines="None"
onselectedindexchanged="GridView1_SelectedIndexChanged"
style="font-family: Tahoma; font-size: small; text-align: center;"
Width="100%" AllowPaging="True" AllowSorting="True"
onpageindexchanging="GridView1_PageIndexChanging">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="DocumentID" HeaderText="Document ID"
InsertVisible="False" ReadOnly="True" SortExpression="DocumentID" />
<asp:BoundField DataField="DocumentType" HeaderText="Document Type"
SortExpression="DocumentType" />
<asp:BoundField DataField="ReceivedDate" HeaderText="Received Date"
SortExpression="ReceivedDate" DataFormatString="{0:dd/MM/yyyy}" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:CommandField SelectText=">>"
ShowSelectButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerSettings PageButtonCount="5" Mode="NumericFirstLast" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
<PagerSettings Mode="NumericFirstLast" PageButtonCount="10" FirstPageText="First" LastPageText="Last"/>
</asp:GridView>
而我的pageindexchanging 是
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//GridView1.DataBind();
}
这是我的SqlDataSource
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT InwardDetails.DocumentID, InwardDetails.Title, DocumentTypeDetails.DocumentType, InwardDetails.ReceivedDate FROM InwardDetails INNER JOIN DocumentTypeDetails ON InwardDetails.DocumentType = DocumentTypeDetails.DocumentTypeID">
</asp:SqlDataSource>
【问题讨论】:
-
为什么要在 Paging 方法中注释 Gridview 绑定?
标签: c# sql sql-server gridview