【问题标题】:Gridview paging in c#, dynamic SQL queryc#中的Gridview分页,动态SQL查询
【发布时间】: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="&gt;&gt;"
    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


【解决方案1】:

从后面的代码绑定网格。而当您使用任何过滤器时,请使用 rowfilter 过滤数据并 再次绑定网格 以提供更新的数据。每次应用或删除过滤器时使用 databind 以获取新数据。

protected void Button1_Click(object sender, EventArgs e)
{

    DataSet ds = new DataSet();
    SqlConnection myCon = new SqlConnection(connectionstring);
    SqlDataAdapter adapter = new SqlDataAdapter(cmd, myCon);
    adapter.Fill(ds);
    DataView view = new DataView();
    view.Table = ds.Tables[0];
    view.RowFilter = "ColumnName = " + TextBox1.Text.Trim();
    GridView1.DataSource = view;
    GridView1.DataBind();
}

【讨论】:

    猜你喜欢
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2012-05-24
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多