【问题标题】:Gridview sort reveals far more than anticipated!Gridview 排序揭示的远远超出预期!
【发布时间】:2009-12-08 14:40:17
【问题描述】:

我需要一些帮助,我目前正在兜圈子。

我有一个基于所选单选按钮项填充的网格视图:

protected void btnSubmit_Click(object sender, EventArgs e)
{

    if (radTopx.SelectedValue == "" || txtbxHowMany.Text == "")
    {
        MessageBox.Show("Please Ensure that BOTH 'The Number of Products' and Appropriate material Is selected Before You Attempt To Run a TOP x Report", "Top x Error!!!",
             MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        GridView1.DataSourceID = null;
        GridView1.DataBind();
    }

    else
    {
        int max = 0;
        if (int.TryParse(txtbxHowMany.Text, out max))
        {

            GridView1.DataSource = this.GetMaterialData(Session["MemberKey"].ToString(), radTopx.SelectedItem.Value, "Primary", max);
            GridView1.DataSourceID = String.Empty;
            GridView1.DataBind();
        }
    }
}

GetMaterialData 代码在哪里:

private object GetMaterialData(string MemberKey, string MaterialType, string MaterialLevel, int Count)

{
    ORWeightsDataClassesDataContext db = new ORWeightsDataClassesDataContext();
    var query = db.tblOnlineReportingCOMPLETEWeights
                .Where(x => x.MemberId == MemberKey && x.MaterialText == MaterialType && x.MaterialLevel == MaterialLevel)
                .OrderByDescending(x => x.ProductPercentage)
                .Take(Count);
    return query;
}

当它在第一个实例中运行时,它运行得非常好并且分页很好......可爱。

但是,当我尝试对 gridview 进行排序时,数据会更改并恢复为从其数据源中引入所有数据集。

这里是gridview背后的html & c#代码:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    style="z-index: 1; left: 215px; top: 560px; position: absolute; height: 133px; width: 755px; text-align: center;" 
           Font-Size="X-Small" 
            onpageindexchanging="GridView1_PageIndexChanging"  onsorting="GridView1_Sorting"    
            AllowPaging="True"  AllowSorting="True">


  protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSourceID = "lqPackWeights";
        GridView1.DataBind();

    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        GridView1.DataSourceID = "lqPackWeights";
        GridView1.DataBind();

    }

有人知道如何确保在对 gridview 进行排序时保持原始视图状态吗?

【问题讨论】:

  • 您在应用排序时是否尝试过重新应用数据源?抱歉,这应该是评论而不是答案。

标签: c# asp.net gridview sorting


【解决方案1】:

您需要检查 GridView1_PageIndexChanging 和 GridView1_Sorting 函数,以查看用户是否对 radTopx 或 txtbxHowMany 进行了任何操作。如果是这样,您将使用 GetMaterialData() 的返回值,否则您将使用“lqPackWeights”的 DataSourceID。

因此,您拥有所需的一切,只需要多一点代码即可处理所有可能出现的各种情况。

【讨论】:

    猜你喜欢
    • 2013-04-02
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 2015-10-06
    • 2021-10-13
    • 2020-02-25
    相关资源
    最近更新 更多