Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Asp:GridView sorting with LINQ
Message
From
15/05/2008 09:46:32
 
 
To
All
General information
Forum:
ASP.NET
Category:
Other
Title:
Asp:GridView sorting with LINQ
Miscellaneous
Thread ID:
01317274
Message ID:
01317274
Views:
52
Something is not working with the "orderby" code when I try to sort my GridView and re-bind using LINQ

When I click the header, the "gvMachines_Sorting" method gets called and the SortExpression of the column is is getting passed to the method, then the LINQ query is run and re-binds, but the OrderBy is not having any effect.

In the LINQ query, I am using "orderby e.SortExpression", but it does not have any effect.

e.SortExpression is a string (I think), so do I need to cast it, and if so, to what type?

from .cs file
     protected void gvMachines_Sorting(object sender, GridViewSortEventArgs e)
        {
 
           DataClasses1DataContext db = new DataClasses1DataContext();

           var Machines = from p in db.machines orderby e.SortExpression select p;
        
            gvMachines.DataSource = Machines;
            gvMachines.DataBind();

        }
    }
aspx file:
    <asp:GridView ID="gvMachines" runat="server" AllowSorting="True" OnSorting="gvMachines_Sorting" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="mach_num" HeaderText="mach_num" SortExpression="mach_num" />
            <asp:BoundField DataField="mach_name" HeaderText="mach_name" SortExpression="mach_name" />
            <asp:BoundField DataField="mach_rate" HeaderText="mach_rate" SortExpression="mach_rate" />
            <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />
            <asp:BoundField DataField="mach_wtd" HeaderText="mach_wtd" SortExpression="mach_wtd" />
            <asp:BoundField DataField="mach_ytd" HeaderText="mach_ytd" SortExpression="mach_ytd" />
            <asp:BoundField DataField="mach_info" HeaderText="mach_info" SortExpression="mach_info" />
            <asp:BoundField DataField="last_rate1" HeaderText="last_rate1" SortExpression="last_rate1" />
            <asp:BoundField DataField="last_date1" HeaderText="last_date1" SortExpression="last_date1" />
            <asp:BoundField DataField="last_rate2" HeaderText="last_rate2" SortExpression="last_rate2" />
            <asp:BoundField DataField="last_date2" HeaderText="last_date2" SortExpression="last_date2" />
            <asp:BoundField DataField="last_rate3" HeaderText="last_rate3" SortExpression="last_rate3" />
            <asp:BoundField DataField="last_date3" HeaderText="last_date3" SortExpression="last_date3" />
            <asp:BoundField DataField="class" HeaderText="class" SortExpression="class" />
            <asp:BoundField DataField="type" HeaderText="type" SortExpression="type" />
            <asp:BoundField DataField="timestamp_column" HeaderText="timestamp_column" InsertVisible="False" SortExpression="timestamp_column" />
        </Columns>
        </asp:GridView>
    
Reply
Map
View

Click here to load this message in the networking platform