set default sort - multiple columns, some hidden

4 posts, 0 answers
  1. Ken
    Ken avatar
    10 posts
    Member since:
    Sep 2011

    Posted 16 Nov 2011 Link to this post

    I have a scenario where the client wants to see "assigned" employees at the top of the list.  An assigned employee has multiple criteria, some of which are based on visible columns, some invisible.  I want this to always be the default sort and not allow the user to change it.  I also don't want to see the arrows over the visible columns.

    Any help would be appreciated!
  2. Ken
    Ken avatar
    10 posts
    Member since:
    Sep 2011

    Posted 16 Nov 2011 Link to this post

    Well, I sort of found a way to do this:

    I added a custom comparer that looks at the columns I need sorted.
    I created a dummy invisible column called Weight and set it to sort descending.
    I assign an instance of the comparer to the grid.

    Is this the best way?
  3. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 17 Nov 2011 Link to this post

    Hello Ken,

    Personally what I would do in this situation is
    • create (in my datasource) a column that indicates "assigned".
    • Make this column not visible to the user
    • Sort by this column programatically (see this help topic)
    • Do not allow sorting on the grid

    this.radGridView1.EnableSorting = false;

     

    Hope that helps
    Richard

     

  4. Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 18 Nov 2011 Link to this post

    Hello Ken,

    The Richard's suggestion goes in the correct direction. I would like to mention that setting the EnableSorting property of RadGridView to 'false' will remove the currently added SortDescriptors. This is why I could suggest you the following approach to achieve your requirements:

    1. Add your visible/invisible columns.
    2. Add the desired SortDescriptors to both visible and invisible columns as required.
    3. Set the AllowSort property of your columns to 'false'. It will not allow the user to change the sorting.
    4. Use the ViewCellFormatting event of RadGridView to hide the sorting arrow:
    private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
    {
        GridHeaderCellElement cell = e.CellElement as GridHeaderCellElement;
        if (cell == null)
        {
            return;
        }
     
        List<RadElement> elements = cell.GetChildrenByType(typeof(ArrowPrimitive));
        if (elements.Count > 0)
        {
            elements[0].Visibility = ElementVisibility.Collapsed;
        }
    }

    I hope it helps in your scenario.

    Best regards,
    Alexander
    the Telerik team

    Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

Back to Top