CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2004
    Posts
    4

    datagrid sort event fires twice

    The datagrid sort event fires twice with the result of this being the order does not change. The asc/desc order switches on the first event and then switches on the second time which actually sets it back to the original order. The basic code pieces are:

    protected int BindResultsToDatagrid(string sortExpression)
    {
    searchResultsDataSet =(DataSet)Session{"searchResultsDataSet"];
    if (sortExpression.Length > 0)
    {
    searchResultsDataSet.Tables[0].DefaultView.Sort = sortExpression;

    Session["searchResultsDataSet"] = searchResultsDataSet;
    }
    searchResults.DataSource = null;
    searchResults.DataBind();

    searchResults.DataSource = searchResultsDataSet.Tables[0].DefaultView;
    searchResults.DataBind();
    return searchResultsDataSet.Tables[0].DefaultView.Count;
    }

    protected void searchResults_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
    string sortExpression = (string)Session["sortExp"];
    string sortDirection = (string)Session["sortDir"];

    if (sortExpression != e.SortExpression)
    {
    sortExpression = e.SortExpression;
    sortDirection = "asc";
    }
    else
    {
    if (sortDirection == "asc")
    sortDirection = "desc";
    else
    sortDirection = "asc";
    }

    Session["sortExp"] = sortExpression;
    Session["sortDir"] = sortDirection;
    BindSearchResultsToGrid(sortExpression + " " + sortDirection);
    }

    I actually copied this code from the net, and I'm surprised it's out there and doesn't work. Can anyone help?

  2. #2
    Join Date
    Jun 2005
    Location
    Maryland,USA
    Posts
    20

    Re: datagrid sort event fires twice

    can you show me the Page Load event?there might be something happening there that's causing the sort to fire twice.

  3. #3
    Join Date
    Nov 2004
    Posts
    4

    Re: datagrid sort event fires twice

    Here's the page load:

    private void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    }
    }

    ...not much help.

  4. #4
    Join Date
    Nov 2004
    Posts
    4

    Re: datagrid sort event fires twice

    I got it figured out! I had OnSortCommand="searchResults_SortCommand" on the ascx page for the datagrid and this.searchResults.SortCommand += new DataGridSortCommandEventHandler(searchResults_SortCommand); in the code behind.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured