sortinggridviewpaginationediting

Paging, sorting, editing with LINQ Data Source


Right, ok, I have followed Scott Gu's example in his part 9, etc, etc blog and despite my best efforts to achieve 'automatic sorting, paging and editing, I cannot get the so and so to work.

Is automatic sorting, paging and editing possible with the following set up?

<asp:TextBox ID="tbxHowMany" runat="server"></asp:TextBox>

    <asp:RadioButtonList ID="radMaterial" runat="server">
        <asp:ListItem>Paper</asp:ListItem>
        <asp:ListItem>Glass</asp:ListItem>
    </asp:RadioButtonList>


    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />


    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" 
        DataSourceID="LQTOPDS" Font-Size="XX-Small" ForeColor="#333333" 
        GridLines="None" DataKeyNames="PriKey" 
        AllowPaging="True" AllowSorting="True">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="Productpriority" HeaderText="Productpriority" 
                SortExpression="Productpriority" />
            <asp:BoundField DataField="MemberId" HeaderText="MemberId" 
                SortExpression="MemberId" />
            <asp:BoundField DataField="UnitWeight" HeaderText="UnitWeight" 
                SortExpression="UnitWeight" />
            <asp:BoundField DataField="WeightUnitCode" HeaderText="WeightUnitCode" 
                SortExpression="WeightUnitCode" />
            <asp:BoundField DataField="RecycledContent" HeaderText="RecycledContent" 
                SortExpression="RecycledContent" />
            <asp:BoundField DataField="IsBiodegradable" HeaderText="IsBiodegradable" 
                SortExpression="IsBiodegradable" />
            <asp:BoundField DataField="Recyclability" HeaderText="Recyclability" 
                SortExpression="Recyclability" />
            <asp:BoundField DataField="RevisionSourceCode" HeaderText="RevisionSourceCode" 
                SortExpression="RevisionSourceCode" />
            <asp:BoundField DataField="PriKey" HeaderText="PriKey" 
                SortExpression="PriKey" ReadOnly="True" />
        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>

    <asp:LinqDataSource ID="LQTOPDS" runat="server" 
        ContextTypeName="LQTOPDSDataContext" EnableInsert="True" EnableUpdate="True" 
        TableName="tblOnlineReportingCOMPLETEWEIGHTSFINALs" 
        Where="MaterialText == @MaterialText &amp;&amp; Productpriority &lt;= @Productpriority">

        <WhereParameters>
            <asp:ControlParameter ControlID="radMaterial" Name="MaterialText" 
                PropertyName="SelectedValue" Type="String" />
            <asp:ControlParameter ControlID="tbxHowMany" Name="Productpriority" 
                PropertyName="Text" Type="Int32" />
        </WhereParameters>
    </asp:LinqDataSource>

Currently the only code behind I have is as follows:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSourceID = null;

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        GridView1.DataSourceID = "LQTOPDS";
            GridView1.DataBind();
    }

}

This is driving me nuts...I'm reading that this sorting/paging/editing functionality should be automatic but obviously I am doing something wrong.

If someone can explain to me why this functionality is not being generated, I would be most grateful.


Solution

  • This was finally achieved by changing the method of creating the table from whcih the data was sourced.

    I removed what was essentially the "SELECT TOP" I was using as a where clause and used a different method. I ranked the data and used 'take' - this effectively selected the data that was preordered and thus removed any additonal selecting that cuased issues with the gridview pging, sorting, etc.

    If anyone would like to see the code, just let me know.