Gridview排序远远超出预期!

时间:2009-12-08 14:40:18

标签: c# asp.net gridview sorting

我需要一些帮助,我现在正在围成一圈。

我有一个基于所选单选按钮项填充的gridview:

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进行排序时,数据会发生变化并恢复为从其数据源中引入所有数据集。

这是html& gridview的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进行排序时,原始视图状态是否得到维护?

1 个答案:

答案 0 :(得分:0)

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

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