This is a migrated thread and some comments may be shown as answers.

set default sort - multiple columns, some hidden

3 Answers 107 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ken
Top achievements
Rank 1
Ken asked on 16 Nov 2011, 06:08 PM
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!

3 Answers, 1 is accepted

Sort by
0
Ken
Top achievements
Rank 1
answered on 16 Nov 2011, 06:32 PM
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?
0
Richard Slade
Top achievements
Rank 2
answered on 17 Nov 2011, 11:23 AM
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

 

0
Alexander
Telerik team
answered on 18 Nov 2011, 10:49 AM
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.

Tags
GridView
Asked by
Ken
Top achievements
Rank 1
Answers by
Ken
Top achievements
Rank 1
Richard Slade
Top achievements
Rank 2
Alexander
Telerik team
Share this question
or