1

嗨,提前感谢,

我正在尝试使用 linq 对 gridview 进行排序,但没有任何反应。我没有收到错误,但视图中也没有进行排序。我也在使用 firebug 进行调试。

ASP:

  <asp:GridView ID="GridViewRangeSetup" runat="server" AllowSorting="True" OnSorting="Gridview_Sort"
                        PagerStyle-Mode="NumericPages" AutoGenerateColumns="false" Width="100%" CssClass="gridView"
                        OnPageIndexChanging="GridViewRangeSetup_PageIndexChanging" AllowPaging="True"
                        PageSize="20" DataKeyNames="RangeId" OnRowCommand="GridViewRangeSetup_RowCommand"
                        OnRowEditing="GridViewRangeSetup_RowEditing" OnRowCancelingEdit="GridViewRangeSetup_CancelEditRow"
                        OnRowUpdating="GridViewRangeSetup_UpdateRow" OnRowDataBound="GridViewRangeSetup_RowDataBound">
                        <RowStyle CssClass="rowStyle"></RowStyle>
                        <HeaderStyle CssClass="headerBar" ForeColor="#ffffff"></HeaderStyle>
                        <AlternatingRowStyle CssClass="altRow" />
                        <Columns>
                            <asp:TemplateField HeaderText="Edit" HeaderStyle-Width="5%" HeaderStyle-ForeColor="#f2f2f2"
                                HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Small">
                                <ItemTemplate>
                                    <asp:ImageButton ID="imgEdit" runat="server" ImageUrl="~/images/icon_edit.png" CausesValidation="false"
                                        CommandArgument='<%#Eval("RangeId") %>' CommandName="Edit" />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:ImageButton ID="imgUpdate" runat="server" ImageUrl="~/images/icon_update.png"
                                        CausesValidation="false" CommandArgument='<%#Eval("RangeId") %>' CommandName="Update" />
                                    <asp:ImageButton ID="ImageCancel" runat="server" ImageUrl="~/images/icon_cancel.png"
                                        CausesValidation="false" CommandArgument='<%#Eval("RangeId") %>' CommandName="Cancel" />
                                </EditItemTemplate>
                                <HeaderStyle Font-Bold="False" Font-Size="Small" ForeColor="#F2F2F2" Width="5%" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Delete" HeaderStyle-Width="3%" HeaderStyle-ForeColor="#f2f2f2"
                                HeaderStyle-Font-Bold="false" HeaderStyle-Font-Size="Small">
                                <ItemTemplate>
                                    <asp:ImageButton ID="imgDelete" runat="server" CausesValidation="false" OnClientClick="return DeleleAlert();"
                                        CommandArgument='<%#Eval("RangeId") %>' CommandName="Remove" ImageUrl="~/images/icon_delete.png" />
                                </ItemTemplate>
                                <HeaderStyle Font-Bold="False" Font-Size="Small" ForeColor="#F2F2F2" Width="3%" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Description" SortExpression="Description">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtDescription" runat="server" CssClass="textbox" Text='<%# Eval("Description") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblDescription" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Country" SortExpression="Country.CountryName">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ddlCountry" runat="server" CssClass="dropdown" AutoPostBack="True"
                                        AppendDataBoundItems="true" DataTextField="CountryName" DataValueField="CountryId">
                                    </asp:DropDownList>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblCountry" runat="server" Text='<%# Bind("CountryName") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="State/Province" SortExpression="GeographicRegion.RegionName">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ddlRegion" runat="server" CssClass="dropdown" AutoPostBack="True"
                                        AppendDataBoundItems="true" DataTextField="RegionName" DataValueField="GeographicRegionId">
                                    </asp:DropDownList>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblRegion" runat="server" Text='<%# Bind("RegionName") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Base/Facility" SortExpression="Base.BaseName">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ddlFacility" runat="server" CssClass="dropdown" AutoPostBack="True"
                                        AppendDataBoundItems="true" DataTextField="BaseName" DataValueField="BaseId">
                                    </asp:DropDownList>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblFacility" runat="server" Text='<%# Bind("BaseName") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Map Name" SortExpression="RangeMap.MapName">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtMapName" runat="server" CssClass="textbox" Text='<%# Eval("MapName") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblMapName" runat="server" Text='<%# Eval("MapName") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Map">
                                <HeaderStyle HorizontalAlign="center" />
                                <ItemTemplate>
                                    <asp:HyperLink ID="HyperLink_Map1" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"MapPath") %>'
                                        Text="">
                                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"MapPath") %>'
                                            Width="50px" Height="50px" />
                                    </asp:HyperLink>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Low Latitude" SortExpression="RangeMap.LowLat">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtLowLat" runat="server" CssClass="textbox" Text='<%# Eval("LowLat") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblLowLat" runat="server" Text='<%# Eval("LowLat") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Low Longitude" SortExpression="RangeMap.LowLong">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtLowLong" runat="server" CssClass="textbox" Text='<%# Eval("LowLong") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblLowLong" runat="server" Text='<%# Eval("LowLong") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="High Latitude" SortExpression="RangeMap.HighLat">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtHighLat" runat="server" CssClass="textbox" Text='<%# Eval("HighLat") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblHighLat" runat="server" Text='<%# Eval("HighLat") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="High Longitude" SortExpression="RangeMap.HighLong">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtHighLong" runat="server" CssClass="textbox" Text='<%# Eval("HighLong") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblHighLong" runat="server" Text='<%# Eval("HighLong") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Status">
                                <ItemTemplate>
                                    <asp:Button ID="RangeSetup_Status" CssClass="page-btn blue" CausesValidation="false"
                                        CommandArgument='<%#Eval("RangeId") %> ' runat="server" Text="Status" OnClick="btnRangeStatus_Click">
                                    </asp:Button>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

C#

受保护的无效Gridview_Sort(对象发送者,GridViewSortEventArgs e){

    //Label2.Text = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
    WISSModel.WISSEntities context = new WISSModel.WISSEntities();


    String column = e.SortExpression;

    IQueryable<dynamic> sortedGridview = ConvertSortDirectionToSql(e.SortDirection) == "ASC" ?
                                                (from r in context.Ranges.AsEnumerable()
                                                 where r.isDeleted == false
                                                 orderby typeof(WISSModel.Range).GetProperty(column).GetValue(r, null) ascending
                                                 select new
                                                 {
                                                     r.RangeId,
                                                     r.Description,
                                                     r.Country.CountryName,
                                                     r.GeographicRegion.RegionName,
                                                     r.Base.BaseName,
                                                     r.RangeMap.MapName,
                                                     r.RangeMap.MapPath,
                                                     r.RangeMap.LowLat,
                                                     r.RangeMap.LowLong,
                                                     r.RangeMap.HighLat,
                                                     r.RangeMap.HighLong
                                                 }).AsQueryable<dynamic>() :
                                                  (from r in context.Ranges.AsEnumerable()
                                                   where r.isDeleted == false
                                                   orderby typeof(WISSModel.Range).GetProperty(column).GetValue(r, null) descending
                                                   select new
                                                   {
                                                       r.RangeId,
                                                       r.Description,
                                                       r.Country.CountryName,
                                                       r.GeographicRegion.RegionName,
                                                       r.Base.BaseName,
                                                       r.RangeMap.MapName,
                                                       r.RangeMap.MapPath,
                                                       r.RangeMap.LowLat,
                                                       r.RangeMap.LowLong,
                                                       r.RangeMap.HighLat,
                                                       r.RangeMap.HighLong
                                                   }).AsQueryable<dynamic>();

    //var sortedGridview = context.Ranges.Where("it.isDeleted == false").OrderBy(column);

    GridViewRangeSetup.DataSource = sortedGridview.ToList();

    //var test = sortedGridview.ToList();

    //System.Diagnostics.Debugger.Break();

    GridViewRangeSetup.DataBind();

}
4

2 回答 2

0

找到另一个解决方案:

使用 DataSetLinqOperators 类并稍微修改它以允许空值:

protected void Gridview_Sort(object sender, GridViewSortEventArgs e)
    {


        WISSModel.WISSEntities context = new WISSModel.WISSEntities();

        var sortedGridview = (from r in context.Ranges
                              where r.isDeleted == false
                              select new
                              {
                                  r.RangeId,
                                  r.Description,
                                  r.Country.CountryName,
                                  r.GeographicRegion.RegionName,
                                  r.Base.BaseName,
                                  r.RangeMap.MapName,
                                  r.RangeMap.MapPath,
                                  r.RangeMap.LowLat,
                                  r.RangeMap.LowLong,
                                  r.RangeMap.HighLat,
                                  r.RangeMap.HighLong
                              }).ToList();

        DataTable sortedTable = sortedGridview.CopyToDataTable();

        sortedTable.DefaultView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

        GridViewRangeSetup.DataSource = sortedTable;

        GridViewRangeSetup.DataBind();

    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        int sort = (int)ViewState["Sort"];
        switch (sort)
        {
            case 0:
                newSortDirection = "ASC";
                ViewState["Sort"] = 1;
                break;

            case 1:
                newSortDirection = "DESC";
                ViewState["Sort"] = 0;
                break;
        }

        return newSortDirection;
    }
于 2013-11-02T20:24:30.760 回答
0

看起来不允许像在动态 T-SQL 中那样按字符串中的参数对顺序进行编码。你应该用这个替换你的 LINQ 查询以使其工作:

String column = e.SortExpression;
IQueryable<dynamic> sortedGridview = ConvertSortDirectionToSql(e.SortDirection) == "ASC" ?
                                            (from r in context.Ranges
                                             where r.isDeleted == false                                              
                                             orderby typeof(Range).GetProperty(column).GetValue(r,null) ascending
                                             select new
                                             {
                                              r.RangeId,
                                              Description = r.Description,
                                              CountryName = r.Country.CountryName,
                                              RegionName = r.GeographicRegion.RegionName,
                                              BaseName = r.Base.BaseName,
                                              r.RangeMap.MapName,
                                              r.RangeMap.MapPath,
                                              r.RangeMap.LowLat,
                                              r.RangeMap.LowLong,
                                              r.RangeMap.HighLat,
                                              r.RangeMap.HighLong
                                             }).AsQueryable<dynamic>() :
                                              (from r in context.Ranges
                                               where r.isDeleted == false                                                  
                                               orderby typeof(Range).GetProperty(column).GetValue(r, null) descending
                                               select new
                                               {
                                              r.RangeId,
                                              Description = r.Description,
                                              CountryName = r.Country.CountryName,
                                              RegionName = r.GeographicRegion.RegionName,
                                              BaseName = r.Base.BaseName,
                                              r.RangeMap.MapName,
                                              r.RangeMap.MapPath,
                                              r.RangeMap.LowLat,
                                              r.RangeMap.LowLong,
                                              r.RangeMap.HighLat,
                                              r.RangeMap.HighLong
                                               }).AsQueryable<dynamic>();
于 2013-10-21T03:58:20.507 回答