使用LINQ数据源进行分页,排序和编辑

时间:2010-01-08 18:07:11

标签: gridview sorting paging editing

是的,好吧,我已经在他的第9部分等博客中关注了Scott Gu的例子,尽管我尽了最大的努力来实现'自动排序,分页和编辑,但我还是不能这样做。

是否可以通过以下设置进行自动排序,分页和编辑?

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

目前我唯一的代码如下:

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

}

这让我疯了......我在读这个排序/分页/编辑功能应该是自动的,但显然我做错了。

如果有人可以向我解释为什么没有生成此功能,我将非常感激。

1 个答案:

答案 0 :(得分:1)

最终通过改变从数据来源创建表的方法来实现。

我删除了基本上用作where子句的“SELECT TOP”,并使用了不同的方法。我对数据进行了排名并使用了“take” - 这有效地选择了预先排序的数据,从而删除了任何添加元素,选择了gridview pging,排序等问题。

如果有人想查看代码,请告诉我。

相关问题