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

Problem with custom sorting in unbound mode.

7 Answers 167 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Raymond
Top achievements
Rank 1
Raymond asked on 29 Nov 2010, 02:43 PM

Hi!

I use 2010.3.10.1109.

I have grid with unbound mode and I want use custom sorting. So I set


this.radGridView1.MasterTemplate.EnableCustomSorting = true;

and I hooked event CustomSorting.

The problem is that in event args property Column is set as deprecated and is always null. CellValue1 and CellValue2 are also deprecated and have null values. I cannot use radGridView1.CurrentColumn because clicking on column header doesn`t change current column.
So I have problem because I don`t know which column has been clicked by the user.

How to solve this problem?


This is my source code:

namespace GridViewUnboundMode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            BuildGrid();
        }
  
        private void BuildGrid()
        {
            for (int index = 0; index < 5; index++)
            {
                GridViewDataColumn newColumn = new GridViewTextBoxColumn();
                newColumn.HeaderText = "Col" + (radGridView1.Columns.Count + 1).ToString();
                newColumn.Name = newColumn.HeaderText;
                radGridView1.Columns.Add(newColumn);
                newColumn.AllowSort = true;
            }
  
            for(int index = 0; index < 10; index++)
            {
                GridViewRowInfo rowInfo = this.radGridView1.Rows.AddNew();
                int columnIndex = 0;
                int rowIndex = rowInfo.Index;
                foreach (GridViewCellInfo cellInfo in rowInfo.Cells)
                {
                    cellInfo.Value = string.Format("{0}-{1}", columnIndex, rowIndex);
                    columnIndex++;
                }
            }
        }
  
        private void radGridView1_CustomSorting(object sender, GridViewCustomSortingEventArgs e)
        {
            if ((e.Row1 != null) && (e.Row2 == null))
            { e.SortResult = 1; }
            else if ((e.Row1 == null) && (e.Row2 != null))
            { e.SortResult = -1; }
            if ((e.Row1 != null) && (e.Row2 != null))
            {
                //radGridView1.CurrentColumn doesn`t help becasue clicking in column header doesn`t change current column
                if ((radGridView1.CurrentColumn != null) && (radGridView1.CurrentColumn.Index >= 0))
                {
                    e.SortResult = e.Row1.Cells[radGridView1.CurrentColumn.Index].Value.ToString().CompareTo(e.Row2.Cells[radGridView1.CurrentColumn.Index].Value.ToString());
                }
            }
        }
  
    }
}

 


I don`t know also why Handled property (from event args) has value true when my handler is executed. I think at the beginning it should have value false and I should set this on true if I want handle sorting.

Regards

Regards

Regards

7 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 29 Nov 2010, 02:54 PM
Hello Raymond, 

Do you have EnableSorting set to true as well? 
You should also have 
this.radGridView1.EnableSorting = true;

otherwise clicking on the header won't do anything. 
You should then be able to get to the args that you need. 
E.g. 
e.Row1.Cells[0].Value.ToString();

Hope this helps, but let me know if you need more information. 
Richard
0
Raymond
Top achievements
Rank 1
answered on 29 Nov 2010, 03:04 PM

It doesn`t solve my problem because I still don`t know which column has been clicked by the user.

So I don`t know which cell from e.Row1 and e.Row2 I should use for sorting.

0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 29 Nov 2010, 03:18 PM
Hello Raymond,

This will get you the current column..
private GridViewColumn m_Column;
private void RadGridView1_CustomSorting(System.Object sender, Telerik.WinControls.UI.GridViewCustomSortingEventArgs e)
{
    MessageBox.Show(m_Column.HeaderText);
}
  
private void RadGridView1_CellClick(System.Object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    m_Column = e.Column;
}

Let me know if you need further help
Richard
0
Raymond
Top achievements
Rank 1
answered on 30 Nov 2010, 11:30 AM
thanks, it helped
but I think clicked column should be avaialbe in event args from CustomSorting event
0
Richard Slade
Top achievements
Rank 2
answered on 30 Nov 2010, 11:32 AM
Hi Raymond, 

I think you're right. Perhaps it's there but it's not obvious to me where it is. I'm glad that I could help though. 
Regards, 
Richard
0
Accepted
Julian Benkov
Telerik team
answered on 02 Dec 2010, 10:47 AM
Hi Raymond,

You can use the SortDescriptors collection of RadGridView for this scenario:

private void radGridView1_CustomSorting(object sender, GridViewCustomSortingEventArgs e)
{
    if ((e.Row1 != null) && (e.Row2 == null))
    {
        e.SortResult = 1;
    }
    else if ((e.Row1 == null) && (e.Row2 != null))
    {
        e.SortResult = -1;
    }
     
    if ((e.Row1 != null) && (e.Row2 != null))
    {
        //radGridView1.CurrentColumn doesn`t help becasue clicking in column header doesn`t change current column
        if ((radGridView1.CurrentColumn != null) && (radGridView1.CurrentColumn.Index >= 0))
        {
            int index = this.radGridView1.Columns.IndexOf(this.radGridView1.SortDescriptors[0].PropertyName);
            e.SortResult = e.Row1.Cells[index].Value.ToString().CompareTo(e.Row2.Cells[index].Value.ToString());
        }
    }
}

I  hope this helps.

Kind regards,
Julian Benkov
the Telerik team
Get started with RadControls for WinForms with numerous videos and detailed documentation.
0
Raymond
Top achievements
Rank 1
answered on 02 Dec 2010, 12:03 PM
Thanks but I used solution from Richard Slade
Your solution also works.
Tags
GridView
Asked by
Raymond
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Raymond
Top achievements
Rank 1
Julian Benkov
Telerik team
Share this question
or