Linq Gridview排序、更新和编辑

Linq Gridview排序、更新和编辑,linq,gridview,sorting,editing,Linq,Gridview,Sorting,Editing,向你们大家致以节日的问候。我真的需要一些帮助,因为这快把我逼疯了 目标。我有一个文本框,客户输入一个数字,他们点击单选按钮列表选择一种材料,点击按钮填充gridview 这是前端代码: <asp:TextBox ID="tbxHowMany" runat="server" style="z-index: 1; left: 300px; top: 250px; position: absolute"></asp:TextBox> <asp:

向你们大家致以节日的问候。我真的需要一些帮助,因为这快把我逼疯了

目标。我有一个文本框,客户输入一个数字,他们点击单选按钮列表选择一种材料,点击按钮填充gridview

这是前端代码:

<asp:TextBox ID="tbxHowMany" runat="server" 
        style="z-index: 1; left: 300px; top: 250px; position: absolute"></asp:TextBox>

    <asp:Button ID="btnDisplayTopReport" runat="server" 
        style="z-index: 1; left: 645px; top: 285px; position: absolute; height: 25px; width: 170px" 
        Text="Display TOP Report" onclick="btnDisplayTopReport_Click" />

    <asp:RadioButtonList ID="radTOP" runat="server" 
        style="z-index: 1; left: 575px; top: 180px; position: absolute; height: 177px; width: 86px">
        <asp:ListItem>Paper</asp:ListItem>
        <asp:ListItem>Glass</asp:ListItem>
        <asp:ListItem>Aluminium</asp:ListItem>
        <asp:ListItem>Steel</asp:ListItem>
        <asp:ListItem>Plastic</asp:ListItem>
        <asp:ListItem>Wood</asp:ListItem>
        <asp:ListItem>Other</asp:ListItem>
    </asp:RadioButtonList>

    <asp:LinqDataSource ID="LQTOPReportDS" runat="server" 
        ContextTypeName="CompleteWeightsDataContext" 
        EnableUpdate="True" TableName="tblOnlineReportingCOMPLETEWeights"
        Where="IDDesc == @IDDesc && UnitUserfield1 == @UnitUserfield1 && UnitUserfield2 == @UnitUserfield2 && MaterialLevel == @MaterialLevel"
        OrderBy="UnitId, MaterialLevel, MaterialText, UnitWeight"
        StoreOriginalValuesInViewState="True">
    </asp:LinqDataSource>

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
        DataKeyNames="PriKey" DataSourceID="LQTOPReportDS" ForeColor="#333333" 
        GridLines="None"  Font-Size="X-Small" 
        style="z-index: 1; left: 5px; top: 375px; position: absolute; height: 133px; width: 187px" 
        onpageindexchanging="GridView1_PageIndexChanging" 
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
            onsorting="GridView1_Sorting">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:CommandField ShowEditButton="True" />
           <asp:BoundField DataField="UnitId" HeaderText="UnitId" 
                SortExpression="UnitId" />
            <asp:BoundField DataField="UnitDescription" HeaderText="UnitDescription" 
                SortExpression="UnitDescription" />
            <asp:BoundField DataField="PackagingGroupId" HeaderText="PackagingGroupId" 
                SortExpression="PackagingGroupId" />
            <asp:CheckBoxField DataField="IsPackagingGroup" HeaderText="IsPackagingGroup" 
                SortExpression="IsPackagingGroup" />
            <asp:BoundField DataField="PackagingTypeCode" HeaderText="PackagingTypeCode" 
                SortExpression="PackagingTypeCode" />
            <asp:BoundField DataField="UnitWeight" HeaderText="UnitWeight" 
                SortExpression="UnitWeight" />
            <asp:BoundField DataField="WeightUnitCode" HeaderText="WeightUnitCode" 
                SortExpression="WeightUnitCode" />
            <asp:BoundField DataField="MaterialLevel" HeaderText="MaterialLevel" 
                SortExpression="MaterialLevel" />
            <asp:BoundField DataField="MaterialText" HeaderText="MaterialText" 
                SortExpression="MaterialText" />
            <asp:BoundField DataField="ProductPercentage" HeaderText="ProductPercentage" 
                SortExpression="ProductPercentage" />
             <asp:BoundField DataField="UnitUserfield2" HeaderText="UnitUserfield2" 
                SortExpression="UnitUserfield2" />
            <asp:BoundField DataField="Comment" HeaderText="Comment" 
                SortExpression="Comment" />

        </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>
现在我知道我必须填充排序事件,但我已经尝试了太多的组合,我已经陷入了一个正确的困境(因此为空!)

我怀疑我会遇到困难,因为它用来填充gridview的linq正在使用take,所以在运行排序时数据可能会丢失


有人能给我指出正确的方向吗?我怎样才能做到这一点?同样地,我知道我在更新和编辑赛艇时也会遇到类似的问题,所以如果我能为达到预期的效果提出建议,我将非常感激……事实上,欢迎你和我一起在我的本地酒吧喝一杯节日饮料

我不完全确定您想做什么,但通常,排序处理程序用于对gridview中的列进行排序。您可以参考下面的示例代码,在其中,我通过升序或降序处理排序,并进行缓存,以便在按列排序时不必再次访问数据库

但通常,您需要更新网格视图的数据源,并使用排序结果重新绑定它

/// <summary>
/// <para>Event handler for sorting items in the grid. It allows the user to sort by any column in the grid.
/// The method makes use of caching to reduce access to the persistence layer.</para>
/// </summary>
/// <param name="sender">The sender which triggered the event.</param>
/// <param name="e">The event arguments.</param>
protected void gvFileList_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortExpression;

    DataSet ds = (DataSet)HttpRuntime.Cache[Session.SessionID + "dsFileList"];
    if (ds == null)
    {
        LoadGridData();
        ds = (DataSet)HttpRuntime.Cache[Session.SessionID + "dsFileList"];
    }
    ds.Tables[0].DefaultView.Sort = e.SortExpression;

    // check the cache to see if user has sorted by this column (i.e. this SortExpression)
    // previously, and if they have, reverse the sort direction
    if (HttpRuntime.Cache[Session.SessionID + "dsFileList" + e.SortExpression] != null)
    {
        // get the previous sort direction from the cache
        string dir = (string)HttpRuntime.Cache[Session.SessionID + "dsFileList" + e.SortExpression];

        // reverse the sort direction and update the cache
        if (dir == "ASC")
        {
            Helper.AddToCache(Session.SessionID + "dsFileList" + e.SortExpression, "DESC", 120);
            ds.Tables[0].DefaultView.Sort += " DESC";
            sortExpression = e.SortExpression + " DESC";
        }
        else
        {
            Helper.AddToCache(Session.SessionID + "dsFileList" + e.SortExpression, "ASC", 120);
            ds.Tables[0].DefaultView.Sort += " ASC";
            sortExpression = e.SortExpression + " ASC";
        }
    }
    else
    {
        // this column has not been sorted on previously, so update the cache with
        // sort direction = ASC for this column since ascending is the initial
        // sort direction
        Helper.AddToCache(Session.SessionID + "dsFileList" + e.SortExpression, "ASC", 120);
        sortExpression = e.SortExpression + " ASC";
    }

    gvFileList.DataSource = ds.Tables[0].DefaultView;
    gvFileList.DataBind();

    // store the sort expression in cache so that when the grid is refreshed by the tmTFFileList_Tick
    // method, we can get the sort expression to use
    Helper.AddToCache(Session.SessionID + "dsFileList.SortExpression", sortExpression, 120);
}
//
///用于对网格中的项进行排序的事件处理程序。它允许用户按网格中的任何列进行排序。
///该方法利用缓存来减少对持久层的访问。
/// 
///触发事件的发件人。
///事件参数。
受保护的无效gvFileList_排序(对象发送方、GridViewSortEventArgs e)
{
字符串排序表达式;
DataSet ds=(DataSet)HttpRuntime.Cache[Session.SessionID+“dsFileList”];
如果(ds==null)
{
LoadGridData();
ds=(数据集)HttpRuntime.Cache[Session.SessionID+“dsFileList”];
}
ds.Tables[0].DefaultView.Sort=e.SortExpression;
//检查缓存,查看用户是否已按此列进行排序(即此SortExpression)
//以前,如果有,则反转排序方向
if(HttpRuntime.Cache[Session.SessionID+“dsFileList”+e.SortExpression]!=null)
{
//从缓存中获取上一个排序方向
string dir=(string)HttpRuntime.Cache[Session.SessionID+“dsFileList”+e.SortExpression];
//反转排序方向并更新缓存
如果(目录==“ASC”)
{
Helper.AddToCache(Session.SessionID+“dsFileList”+e.SortExpression,“DESC”,120);
ds.Tables[0].DefaultView.Sort+=“DESC”;
sortExpression=e.sortExpression+DESC;
}
其他的
{
Helper.AddToCache(Session.SessionID+“dsFileList”+e.SortExpression,“ASC”,120);
ds.Tables[0].DefaultView.Sort+=“ASC”;
sortExpression=e.sortExpression+“ASC”;
}
}
其他的
{
//此列以前未进行排序,因此请使用更新缓存
//此列的排序方向=ASC,因为升序是初始值
//排序方向
Helper.AddToCache(Session.SessionID+“dsFileList”+e.SortExpression,“ASC”,120);
sortExpression=e.sortExpression+“ASC”;
}
gvFileList.DataSource=ds.Tables[0].DefaultView;
gvFileList.DataBind();
//将排序表达式存储在缓存中,以便在tmTFFileList刷新网格时
//方法,我们可以获得要使用的排序表达式
Helper.AddToCache(Session.SessionID+“dsFileList.SortExpression”,SortExpression,120);
}
/// <summary>
/// <para>Event handler for sorting items in the grid. It allows the user to sort by any column in the grid.
/// The method makes use of caching to reduce access to the persistence layer.</para>
/// </summary>
/// <param name="sender">The sender which triggered the event.</param>
/// <param name="e">The event arguments.</param>
protected void gvFileList_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortExpression;

    DataSet ds = (DataSet)HttpRuntime.Cache[Session.SessionID + "dsFileList"];
    if (ds == null)
    {
        LoadGridData();
        ds = (DataSet)HttpRuntime.Cache[Session.SessionID + "dsFileList"];
    }
    ds.Tables[0].DefaultView.Sort = e.SortExpression;

    // check the cache to see if user has sorted by this column (i.e. this SortExpression)
    // previously, and if they have, reverse the sort direction
    if (HttpRuntime.Cache[Session.SessionID + "dsFileList" + e.SortExpression] != null)
    {
        // get the previous sort direction from the cache
        string dir = (string)HttpRuntime.Cache[Session.SessionID + "dsFileList" + e.SortExpression];

        // reverse the sort direction and update the cache
        if (dir == "ASC")
        {
            Helper.AddToCache(Session.SessionID + "dsFileList" + e.SortExpression, "DESC", 120);
            ds.Tables[0].DefaultView.Sort += " DESC";
            sortExpression = e.SortExpression + " DESC";
        }
        else
        {
            Helper.AddToCache(Session.SessionID + "dsFileList" + e.SortExpression, "ASC", 120);
            ds.Tables[0].DefaultView.Sort += " ASC";
            sortExpression = e.SortExpression + " ASC";
        }
    }
    else
    {
        // this column has not been sorted on previously, so update the cache with
        // sort direction = ASC for this column since ascending is the initial
        // sort direction
        Helper.AddToCache(Session.SessionID + "dsFileList" + e.SortExpression, "ASC", 120);
        sortExpression = e.SortExpression + " ASC";
    }

    gvFileList.DataSource = ds.Tables[0].DefaultView;
    gvFileList.DataBind();

    // store the sort expression in cache so that when the grid is refreshed by the tmTFFileList_Tick
    // method, we can get the sort expression to use
    Helper.AddToCache(Session.SessionID + "dsFileList.SortExpression", sortExpression, 120);
}