Question

Hi and thanks in advance,

I am trying to sort a gridview with linq and nothing is happening. I am not getting an error, but no sorting is happening in the view either. I am also using firebug for debugging as well.

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#

protected void Gridview_Sort(object sender, 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();

}
Was it helpful?

Solution 2

Found another solution:

Using the DataSetLinqOperators class and modifying it slightly to allow null values:

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

OTHER TIPS

Looks like it is not allowed to code order by parameters in string like in dynamic T-SQL. You should replace yout LINQ query with this one to make it work:

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>();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top