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

sort radgridview column when clicking on another column's header

3 Answers 312 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Danilo
Top achievements
Rank 1
Danilo asked on 05 Jan 2015, 07:16 AM
Hello everyone

I'm trying to sort a RadGridView by clicking a header cell. The name of the column which should be sorted is "priority" and it's type is double. I have some other column which has different values. All these columns together build a "priority number" like 1.0, 2.0, 2.1 etc. And this "priority number" is in column called "priority". And now I'm trying to sort the priority column. It shouldn't matter which column header i click, it just have to sort the priority column. I tried this code:

private void radGridView1_CellClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    if (e.RowIndex == -1)
    {
        if (e.Column.Name == "va")
        {
            if (radGridView1.Columns["priority"].SortOrder == RadSortOrder.Ascending)
            {
                radGridView1.Columns["priority"].Sort(RadSortOrder.Descending, false);
            }

            else if (radGridView1.Columns["priority"].SortOrder == RadSortOrder.Descending)
            {
                radGridView1.Columns["priority"].Sort(RadSortOrder.Ascending, false);
            }

            else
            {
                radGridView1.Columns["priority"].Sort(RadSortOrder.Ascending, false);
            }
        }
    }
}

But it doesn't work. When I hit "va" column header, it sorts the "va" column and not the "priority" column. But in debugging mode I can see it goes through this code. Can anyone please tell me why it's not sorting "priority" column?

Thank you

Regards,
Danilo

3 Answers, 1 is accepted

Sort by
0
Danilo
Top achievements
Rank 1
answered on 07 Jan 2015, 10:41 AM
Okay I almost fixed it. I'm using Custom_Sorting() event from the UI Documentation now. This is my code:

private void radGridView1_CustomSorting(object sender, GridViewCustomSortingEventArgs e)
{
    decimal priority1 = Convert.ToDecimal(e.Row1.Cells["priority"].Value.ToString());
    decimal priority2 = Convert.ToDecimal(e.Row2.Cells["priority"].Value.ToString());

    if (priority1 > priority2)
    {
        e.SortResult = 1;
    }

    else if (priority1 < priority2)
    {
        e.SortResult = -1;
    }

    else
    {
        e.SortResult = 0;
    }
}

But I noticed it's only sorting ascending. It doesn't matter if I set the priority column to Asc, desc or none, it always sorts ascending when I click on header cell. Can someone please explain me why and how to fix it?

Regards,
Danilo
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 07 Jan 2015, 04:33 PM
Hello Danilo,

Thank you for writing.

As you have already found out, the custom sorting is a flexible mechanism for sorting RadGridView rows using custom logic. The CustomSorting event is fired once you click over the header cell and a SortDescriptor is added. In order to perform the correct sort order, you should inverse the GridViewCustomSortingEventArgs.SortResult if the SortDescriptor.Direction property is set to ListSortDirection.Descending:
public Form1()
{
    InitializeComponent();
 
    DataTable dt = new DataTable();
    dt.Columns.Add("Num1", typeof(double));
    dt.Columns.Add("Num2", typeof(double));
    dt.Columns.Add("Num3", typeof(double));
    dt.Columns.Add("Priority", typeof(double));
 
    Random rand = new Random();
    for (int i = 0; i < 10; i++)
    {
        dt.Rows.Add(rand.Next(0, 10) * 0.15, rand.Next(0, 10) * 0.15, rand.Next(0, 10) * 0.15, rand.Next(10, 100) * 0.15);
    }
 
    this.radGridView1.DataSource = dt;
    this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
 
    this.radGridView1.EnableCustomSorting = true;
    this.radGridView1.CustomSorting += radGridView1_CustomSorting;
}
 
private void radGridView1_CustomSorting(object sender, Telerik.WinControls.UI.GridViewCustomSortingEventArgs e)
{
    decimal priority1 = Convert.ToDecimal(e.Row1.Cells["priority"].Value.ToString());
    decimal priority2 = Convert.ToDecimal(e.Row2.Cells["priority"].Value.ToString());
 
    if (priority1 > priority2)
    {
        e.SortResult = 1;
    }
    else if (priority1 < priority2)
    {
        e.SortResult = -1;
    }
    else
    {
        e.SortResult = 0;
    }
 
    if (this.radGridView1.SortDescriptors.Count > 0 &&
        this.radGridView1.SortDescriptors.First().Direction == ListSortDirection.Descending)
    {
        e.SortResult *= -1;
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Danilo
Top achievements
Rank 1
answered on 08 Jan 2015, 07:13 AM
Hi Desislava

Thank you very much. The part you added at the ending of Custom_Sorting() event helped. It's working fine now.

Regards
Roman
Tags
GridView
Asked by
Danilo
Top achievements
Rank 1
Answers by
Danilo
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or