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

时间:2021-12-08 15:38:04

I'm trying to make my gridview sortable. I have allow sorting and added a onsorting attribute given below

我正在尝试使我的gridview可排序。我允许排序并添加下面给出的onsorting属性

<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>

This is my c# code i used to enable the sorting

这是我用来启用排序的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");
        }
    } 

The tutorial i follow required a data layer which i didnt use as i have binded my gridview with boundfield. However when trying to get my datasource they gave me the error stated above.

我遵循的教程需要一个我没有使用的数据层,因为我已经使用boundfield绑定了我的gridview。但是当我试图获取我的数据源时,他们给了我上面提到的错误。

This is how i bind my gridview

这就是我绑定gridview的方式

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();
    }

1 个解决方案

#1


1  

Your problem is the following line of code:

您的问题是以下代码行:

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

This is telling the GridView to bind to something that is not the same type of data source you originally bound the grid to. For example, if the user had selected to sort by clicking the Victim column, then the above code would try to set a string of victim ascending as the data source, which clearly is incorrect.

这告诉GridView绑定到与最初绑定网格的数据源类型不同的内容。例如,如果用户选择通过单击Victim列进行排序,则上述代码将尝试将受害者升序字符串设置为数据源,这显然是不正确的。

To get quick paging/sorting capabilities into a GridView the easiest way is to use the DataTable data structure, along with a DataView data structure.

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

A previous * question details an example of doing this. The question itself details the usage of DataTable and DataView to facilitate the sorting and paging, along with using ViewState to maintain whether the sorting direction is ascending or descending.

之前的*问题详细说明了这样做的一个示例。问题本身详细介绍了DataTable和DataView的使用,以便于排序和分页,以及使用ViewState来维护排序方向是升序还是降序。

Read Enable Sorting and Paging in Gridview Efficiently to see the code example.

阅读在Gridview中启用排序和分页有效地查看代码示例。

To more appropriately handle paging/sorting, you would want to incorporate custom paging and sorting instead of the AllowPaging=True and AllowSorting=True settings on the GridView. The reason for this is that those GridView settings still ask for all of the rows from whatever data source you are using (usually a database) and loads it all into memory, which if it is 20 rows that is not so bad, but imagine 10,000 rows of data; it will be slow and inefficient use of memory. True paging will only get the exact amount of rows to show on the page and then ask for another page chunk when the user navigates; it is more chatty with calling the database more than once, but it is extremely fast and efficient memory-wise.

为了更好地处理分页/排序,您需要在GridView上合并自定义分页和排序,而不是AllowPaging = True和AllowSorting = True设置。原因是那些GridView设置仍然要求您使用的任何数据源(通常是数据库)中的所有行并将其全部加载到内存中,如果它是20行并不是那么糟糕,但想象10,000数据行;它会缓慢而低效地使用内存。真正的分页只会获得要在页面上显示的确切行数,然后在用户导航时请求另一个页面块;不止一次调用数据库更加健谈,但它在内存方面非常快速有效。

#1


1  

Your problem is the following line of code:

您的问题是以下代码行:

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

This is telling the GridView to bind to something that is not the same type of data source you originally bound the grid to. For example, if the user had selected to sort by clicking the Victim column, then the above code would try to set a string of victim ascending as the data source, which clearly is incorrect.

这告诉GridView绑定到与最初绑定网格的数据源类型不同的内容。例如,如果用户选择通过单击Victim列进行排序,则上述代码将尝试将受害者升序字符串设置为数据源,这显然是不正确的。

To get quick paging/sorting capabilities into a GridView the easiest way is to use the DataTable data structure, along with a DataView data structure.

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

A previous * question details an example of doing this. The question itself details the usage of DataTable and DataView to facilitate the sorting and paging, along with using ViewState to maintain whether the sorting direction is ascending or descending.

之前的*问题详细说明了这样做的一个示例。问题本身详细介绍了DataTable和DataView的使用,以便于排序和分页,以及使用ViewState来维护排序方向是升序还是降序。

Read Enable Sorting and Paging in Gridview Efficiently to see the code example.

阅读在Gridview中启用排序和分页有效地查看代码示例。

To more appropriately handle paging/sorting, you would want to incorporate custom paging and sorting instead of the AllowPaging=True and AllowSorting=True settings on the GridView. The reason for this is that those GridView settings still ask for all of the rows from whatever data source you are using (usually a database) and loads it all into memory, which if it is 20 rows that is not so bad, but imagine 10,000 rows of data; it will be slow and inefficient use of memory. True paging will only get the exact amount of rows to show on the page and then ask for another page chunk when the user navigates; it is more chatty with calling the database more than once, but it is extremely fast and efficient memory-wise.

为了更好地处理分页/排序,您需要在GridView上合并自定义分页和排序,而不是AllowPaging = True和AllowSorting = True设置。原因是那些GridView设置仍然要求您使用的任何数据源(通常是数据库)中的所有行并将其全部加载到内存中,如果它是20行并不是那么糟糕,但想象10,000数据行;它会缓慢而低效地使用内存。真正的分页只会获得要在页面上显示的确切行数,然后在用户导航时请求另一个页面块;不止一次调用数据库更加健谈,但它在内存方面非常快速有效。