Why is my Datagrid Sort not working?

Discussion in 'ASP .Net Datagrid Control' started by anonieko@hotmail.com, Feb 7, 2005.

  1. Guest

    >
    >
    >
    >


    This applies to ASPNET 1.1 and it took me a bit of time figuring
    out cause of the problem
    ---------------------------

    I enabled sorting. I have a method that
    handles the SortCommand and it fires ok.

    After getting the view and assigning the field "Sort"
    of the dataview with the correct expression,
    the datagrid still doesn't sort.


    Well, here is one reason. Look the the HTML of the datagrid.

    If the "DataSource" attribute is defined, then you have to
    remove it. For example, the following has the attribute "DataSource"
    define.

    <asp:datagrid id=TransactionsDataGrid runat="server"
    AllowSorting="True" AutoGenerateColumns="False" AllowPaging="True"
    CellPadding="2" BackColor="White" BorderWidth="1px" BorderStyle="None"
    BorderColor="#3366CC" Width="100%" ShowFooter="True" DataSource="<%#
    transactionsDataSet %>">


    After removing that attribute, try to see if it would now work:

    Here are some snippets: (for reference)
    =========================================

    private void DataGrid1_SortCommand(object source,
    System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
    Session["PrevSort"] = Session["Sort"];
    Session["Sort"] = (string) e.SortExpression;

    // Logic to reverse the sort when same link is clicked again.
    string prevSort = (string) Session["PrevSort"];
    string currSort = (string) Session["Sort"];

    if ( prevSort.Replace("DESC", "").Trim() ==
    currSort.Replace("DESC", "").Trim() )
    {
    if ( prevSort.EndsWith("DESC") )
    currSort.Replace("DESC", "");
    else
    currSort += " DESC";
    }
    Session["Sort"] = currSort;

    BindGrid();
    }



    private DataView CreateDataSource()
    {
    sqlConnection1.ConnectionString = GetConnectionString();
    sqlDataAdapter1.Fill(this.transactionsDataSet);
    DataView dv = transactionsDataSet.Tables[0].DefaultView;
    string sortKey = (string) Session["Sort"];


    dv.Sort = sortKey;
    return dv;
    }

    private void BindGrid()
    {
    // Assign datasource only here. Remove any assignment to
    attribute 'DataSource' in the HTML
    TransactionsDataGrid.DataSource = CreateDataSource();

    // This will attemp to Bind. If pageindex is out of synch, it
    resets to first page.
    try
    {
    TransactionsDataGrid.DataBind();
    }
    catch( HttpException exc)
    {
    if ( exc.Message.StartsWith("Invalid CurrentPageIndex
    value") )
    {
    TransactionsDataGrid.CurrentPageIndex = 0;
    TransactionsDataGrid.DataBind();
    }
    }
    }


    private void DataGrid1_PageIndexChanged(object source,
    System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    TransactionsDataGrid.CurrentPageIndex = e.NewPageIndex;
    BindGrid();
    }
     
    , Feb 7, 2005
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ThunderMusic
    Replies:
    4
    Views:
    4,935
    ThunderMusic
    Jan 27, 2006
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,386
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,853
    Smokey Grindel
    Dec 2, 2006
  4. Skybuck Flying
    Replies:
    16
    Views:
    946
    tragomaskhalos
    Aug 25, 2007
  5. pietaster

    ItemCommand not working when datagrid can page and sort

    pietaster, Apr 7, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    13
    Views:
    538
    pietaster
    Apr 15, 2004
  6. Anonieko Ramos

    Why is my Paging not working in my DataGrid?

    Anonieko Ramos, Jul 31, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    204
    Anonieko Ramos
    Jul 31, 2004
  7. Navin
    Replies:
    1
    Views:
    1,136
    Ken Schaefer
    Sep 9, 2003
  8. GIMME
    Replies:
    5
    Views:
    449
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
Loading...