Sorting a Data Grid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbewers1
    New Member
    • Feb 2009
    • 68

    Sorting a Data Grid

    Hi there

    I'm trying to add in sorting for the data grid in my web page but nothing happens when I click on the table headers.

    I've researched the web and developed the code below and set AllowSorting to true in the Properties box in Visual Studio. I've also tried debugging this and setting a breakpoint but the method is never hit.

    Should I need to call something from my OnPreRender or another part of the ASP.LifeCycle?

    Code:
    protected void contactsGrid_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            DataTable dt = WebStaffManager.Instance.SearchDefault(FirstNameBox.Text, 
            SurnameBox.Text, PhoneBox.Text);     
    
            DataView dv = new DataView(dt);
            dv.AllowNew = false;
            dv.AllowDelete = false;
            dv.AllowEdit = false;
            dv.Sort = ViewState["sorting"].ToString();
    
            if ((ViewState.Count % 2) == 0)
            {
                dv.Sort = e.SortExpression + " " + "ASC";
            }
            else
            {
                dv.Sort = e.SortExpression + " " + "DESC";
            }
    
            ViewState["sorting"] = dv.Sort;
            contactsGrid.DataSource = dv;
            contactsGrid.DataBind();
        }
  • IanWright83
    New Member
    • Apr 2009
    • 9

    #2
    Maybe you should post this in the ASP forum?

    Comment

    Working...