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

Sort on hidden column

7 Answers 268 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marc
Top achievements
Rank 2
Marc asked on 04 Jan 2009, 03:34 AM
I have searched your forum and documentation and simply cannot find a complete answer to this common grid action.

Can I please have an example of how to allow the user to click on a column header to sort, but actually sort on a hidden column.  I would also like to maintain the sort-arrow glyphs on the visible (dummy) column.

I use VB.NET with VS2008.

PS:  I love your controls but really request more sample code to common solutions.

7 Answers, 1 is accepted

Sort by
0
Deyan
Telerik team
answered on 05 Jan 2009, 01:37 PM
Hello Marc,

Thank you for your further interest in RadControls for WinForms and in RadGridView in particular.

It is possible to sort the grid based on a hidden column when the user clicks on any visible column in the header. I have prepared a code snippet that demonstrates how to achieve this by enabling the CustomDataOperation option for the visible column that sorts the hidden one, and providing a handler for the CustomSort event of the RadGridView.



Follow these steps to achieve the desired behavior after you have bound your grid with your datasource:

1. Use the property grid for the RadGridView and find the MasterGridViewTemplate property.

2. In the MasterGridViewTemplate subtree find the Columns property and open the GridViewDataColumn Collection Editor for the property.

3. Hide the column that you want by setting the IsVisible property to false

   Note: By removing the column from the collection you will not be able to access it later when performing
            a custom sort operation. That is why we use the IsVisible property in order to hide the column in         
            question.

4. Now select the column which will sort the grid based on the hidden one and set its CustomDataOperation property to "Sorting".

5. Click "OK" on the GridViewDataColumn Collection Editor window.

6. Now select the RadGridView and open its Event list in the property grid; find the CustomSorting event and provide a handler for it.

7. In the CustomSorting event handler write the following code:

 
 
    * Private Sub On_CustomSorting(ByVal sender As ObjectByVal e As GridViewCustomSortingEventArgs) 
    *     'Read the value of the hidden column for each two rows which are compared 
    *     'during the sorting operation. 
    *     Dim value1 As String = TryCast(Me.radGridView1.Rows(e.RowIndex1).Cells("<NameOfTheHiddenColumn>").Value, String
    *     Dim value2 As String = TryCast(Me.radGridView1.Rows(e.RowIndex2).Cells("<NameOfTheHiddenColumn>").Value, String
    *     
    *     'Check the sort order 
    *     If e.Column.SortOrder = RadSortOrder.Ascending Then 
    *         'Custom compare logic for the two values 
    *         e.SortResult = value1.CompareTo(value2) 
    *     Else 
    *         'Custom compare logic for the two values 
    *         e.SortResult = value1.CompareTo(value2) * (-1) 
    *     End If 
    * End Sub 
 
 

In this way, when a Sort Operation is requested for the column which has its CustomDataOperation property set to Sorting, the grid will be sorted based on the hidden column.

I hope this will help you through.

Do not hesitate to write us back if you need further assistance, have any suggestions or feedback.

Kind regards,
Author Deyan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Marc
Top achievements
Rank 2
answered on 05 Jan 2009, 11:58 PM
I am using the grid in Unbound mode and create the columns in the grid programmatically.  I have been able to set the properties as you suggested and this does work, but the when I repopulate the grid (clear the rows and readd) this generates an error.

I tried setting the CustomDataOperation back to none before repopulating, but this too generates an error.

Am I to assume this is not possible when using the grid in unbound mode?

0
Deyan
Telerik team
answered on 08 Jan 2009, 05:53 PM
Hi Marc,

Unfortunately, there is an issue within RadGridView which prevents the user to implement Custom Sorting Operation in Unbound Mode. We are aware of this issue and are going to address it in one of our next releases.

Meanwhile, I would suggest implementing a custom DataSource object and bind RadGridView to it. Then the Custom Sorting will work as expected.

I hope this helps.

Do not hesitate to contact us if you need more help. I would also like to ask you for any feedback or suggestions regarding RadControls for WinForms.

Best wishes,
Deyan
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Guy
Top achievements
Rank 1
answered on 12 Sep 2009, 05:33 AM
Hi ,
Same problem. do you have a fix?

Guy
0
Deyan
Telerik team
answered on 15 Sep 2009, 07:16 AM
Hello Guy,

We plan to refactor the Data Layer of RadGridView control in Q3 2009 release of RadControls for WinForms. This will lead to significant performance improvements and issues similar to the one mentioned in this thread will be addressed, as well. So my advise is to stay tuned and check our website for any updates in our new release. Thanks for your time.

Best wishes,
Deyan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Subh
Top achievements
Rank 1
answered on 01 Oct 2009, 05:58 AM
Hi,
   I'm using Q2 2009 version. I have  a imagecolumn which needs to be sorted based on the hidden numeric column. The custom sort works fine on the hidden column but when the grid is grouped or filtered and then if the sorting is done, the below exception is thrown.

System.ArgumentException was unhandled by user code
  Message="Expecting 1 value(s) for the key being indexed, but received 2 value(s)."

Please let me know if there is any other work round.

Thanks in advance
Subh
0
Deyan
Telerik team
answered on 05 Oct 2009, 07:20 AM
Hello Subh,

Thanks for writing. As I said earlier in my post, we are entirely rewriting the Data Layer of our RadGridView control. We will do our best to address this issue as well. You can check our website for updates on our new releases.

All the best,
Deyan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
Marc
Top achievements
Rank 2
Answers by
Deyan
Telerik team
Marc
Top achievements
Rank 2
Guy
Top achievements
Rank 1
Subh
Top achievements
Rank 1
Share this question
or