views:

87

answers:

1

Hello all, I need some help here, I'm currently going round in circles.

I have a gridview that is populated based upon a radio button item selected :

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

Where the GetMaterialData code is:

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

When this is run in the first instance, this runs absolutely fine and is paged fine...lovely.

However, when I attempt to sort the gridview, the data changes and reverts back to bringing in all the data set from it's data source.

Here is the html & c# code behind for the gridview:

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

    }

Anyone have any ideas how I can ensure that when the gridview is sorted, the original viewstate is maintained?

A: 

You need to have a check in both your GridView1_PageIndexChanging and GridView1_Sorting functions to see if the user has done anything with radTopx or txtbxHowMany. If so, you would use the returned value of GetMaterialData(), otherwise you would use the DataSourceID of "lqPackWeights".

So you have everything you need, it just takes a little more code to handle all of the various situations that can arise.

Justin C