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

Sorting Gridview according to combination of 3 hidden column.

1 Answer 56 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jayesh V
Top achievements
Rank 1
Jayesh V asked on 02 Sep 2011, 02:36 PM
Hi,
We are creating a winform application.
We are binding radgridview with auto generated columns. so we have a IList<PClass> for binding radGridview with columns Id(int), Year(int), dept (Char), DeptNumber (autoincrement int for each dept), ProposalId(Combination of year/OF/deptChar/DeptNumber as string), Name(string), date(date without time).
In radGridview id, year, dept and DeptNumber is hidden, and other is visible.
so my problem is, when I click to ProposalId column header to sort that column, I want to actually sort with combination of year, dept and deptNumber column wise.
here by default
       year => descending
       DeptChar => ascending
       DeptNumber => descending

but when i click it  toggle the sort
so how can we sort with this situation?

thanks in advance,

Jayesh Varu.

1 Answer, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 07 Sep 2011, 05:53 PM
Hello Jayesh V,

You can handle the SortChanged event to achieve this functionality. Here is a simple code snippet which uses the AdventureWorks database:

void radGridView1_SortChanged(object sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
{
    using (this.radGridView1.DeferRefresh())
    {
        string[] defaultSorting = { "RevisionNumber", "OrderDate" };
        ListSortDirection[] defaultOrder = { ListSortDirection.Descending, ListSortDirection.Ascending };
 
        for (int i = 0; i < defaultSorting.Length; i++)
        {
            int index = this.radGridView1.SortDescriptors.IndexOf(defaultSorting[i]);
            if (index >= 0)
            {
                this.radGridView1.SortDescriptors.RemoveAt(index);
            }
        }
 
        for (int i = defaultSorting.Length - 1; i >= 0; i--)
        {
            this.radGridView1.SortDescriptors.Insert(0, new SortDescriptor(defaultSorting[i], defaultOrder[i]));
        }
    }
}

I hope this helps. Do not hesitate to contact us if you have further questions.

Regards,
Julian Benkov
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
Jayesh V
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Share this question
or