排序时,数据源不支持服务器端数据分页

本文关键字:服务器端 数据 分页 不支持 数据源 排序 | 更新日期: 2023-09-27 18:26:29

我正在努力使我的网格视图可排序。我已经允许排序,并在下面添加了一个onstarting属性

<asp:GridView ID="GWCase" runat="server"  DataKeyNames="detail, propertydetail,suspectdetail " BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" Width="100%" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GWCase_SelectedIndexChanged" AutoGenerateColumns="False" AllowPaging="True" PageSize="5" OnPageIndexChanging="GWCase_PageIndexChanging" AllowSorting="True" OnSorting="gridView_Sorting" CurrentSortField="memberreportid" CurrentSortDirection="ASC">
    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
    <RowStyle BackColor="White" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
<Columns>
     <asp:BoundField DataField="memberreportid" HeaderText="MemberReportID" SortExpression="memberreportid"/>
     <asp:BoundField DataField="typeofcrime" HeaderText="Type of Crime" SortExpression="typeofcrime" />
     <asp:BoundField DataField="crdatetime" HeaderText="ReportDateTime" SortExpression="crdatetime" />
     <asp:BoundField DataField="address" HeaderText="Address" SortExpression="address" />
     <asp:BoundField DataField="incidentdate" HeaderText="Incident Date" SortExpression="incidentdate" />
     <asp:BoundField DataField="incidenttime" HeaderText="Incident Time" SortExpression="incidenttime"/>
     <asp:BoundField DataField="property" HeaderText="Property" SortExpression="Property"/>
     <asp:BoundField DataField="victim" HeaderText="Victim" SortExpression="victim" />
     <asp:BoundField DataField="suspect" HeaderText="Suspect" SortExpression="suspect"/>
     <asp:BoundField DataField="detail" HeaderText="Detail" SortExpression="detail" Visible="false"/>
</Columns>
</asp:GridView>

这是我用来启用排序的c#代码

protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        SortDirection sortDirection = SortDirection.Ascending;
        string sortField = string.Empty;
        SortGridview((GridView)sender, e, out sortDirection, out sortField);
        string strSortDirection = e.SortDirection == SortDirection.Ascending ? "ASC" : "DESC";
        //Error happens here
        GWCase.DataSource = (e.SortExpression + " " + strSortDirection);
        GWCase.DataBind();
    }

    private void SortGridview(GridView gridView, GridViewSortEventArgs e, out SortDirection sortDirection, out string sortField)
    {
        sortField = e.SortExpression;
        sortDirection = e.SortDirection;
        if (gridView.Attributes["CurrentSortField"] != null && gridView.Attributes["CurrentSortDirection"] != null)
        {
            if (sortField == gridView.Attributes["CurrentSortField"])
            {
                if (gridView.Attributes["CurrentSortDirection"] == "ASC")
                {
                    sortDirection = SortDirection.Descending;
                }
                else
                {
                    sortDirection = SortDirection.Ascending;
                }
            }
            gridView.Attributes["CurrentSortField"] = sortField;
            gridView.Attributes["CurrentSortDirection"] = (sortDirection == SortDirection.Ascending ? "ASC" : "DESC");
        }
    } 

我遵循的教程需要一个数据层,我没有使用它,因为我已经将我的网格视图与边界字段绑定。然而,当试图获取我的数据源时,他们给了我上面所说的错误。

这就是我如何绑定我的网格视图

private void LoadGrid()
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
        conn.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter("SELECT memberreportid, typeofcrime, crdatetime, address, detail, incidentdate, incidenttime, property, victim, suspect, detail, suspectdetail, propertydetail from memberreport where assignto = 'unassign'", conn);
        da.Fill(ds);
        GWCase.DataSource = ds.Copy();
        GWCase.DataBind();
        conn.Close();
    }

排序时,数据源不支持服务器端数据分页

您的问题是以下代码行:

GWCase.DataSource = (e.SortExpression + " " + strSortDirection);

这是告诉GridView绑定到与您最初将网格绑定到的数据源类型不同的数据源。例如,如果用户通过单击Victim列选择排序,那么上面的代码会尝试将一个字符串victim ascending设置为数据源,这显然是不正确的。

要在GridView中获得快速分页/排序功能,最简单的方法是使用DataTable数据结构和DataView数据结构。

前面的StackOverflow问题详细介绍了这样做的一个示例。问题本身详细说明了使用DataTableDataView来促进排序和分页,以及使用ViewState来保持排序方向是升序还是降序。

请阅读EnableSortingandPaging in Gridview Efficiently以查看代码示例。

为了更恰当地处理分页/排序,您希望在GridView上加入自定义分页和排序,而不是AllowPaging=TrueAllowSorting=True设置。原因是这些GridView设置仍然要求从您使用的任何数据源(通常是数据库)中获取所有行,并将其全部加载到内存中,如果是20行,那也没那么糟糕,但想象一下有10000行数据;这将是对内存的缓慢而低效的使用。真正的分页只会得到要在页面上显示的确切行数,然后在用户导航时请求另一个页面块;它可以多次调用数据库,但在内存方面非常快速高效。