)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to sort a column by display member rather doing it by value member in WinForms GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: May 4, 2016 |
Last Revised Date: February 12, 2020

Sort column by displaymember

The default behavior of the sorting method is relying on the data source for the grid and by default sorting is done based on the value members present in the data source and not based on display member. It can be achieved by adding the foreign key reference to the grid. The code for foreign key column can be added to the view of the data table so that the sort behavior can be redirected to use foreign key column linked to the combo box column when the user sorts combo box column.
We have provided the step by step procedure to implement the SortByDisplayMember functionality in GridGroupingControl. Moreover, here we have provided the simple sample. In this sample, we have explained the SortByDisplayMember by ComboBox column and comment out the process for each step.

C#

//Creating the Foreign key table
GridRelationDescriptor rd = new GridRelationDescriptor();
rd.Name = "ChoiceList"; //just some unique name
rd.RelationKind = RelationKind.ForeignKeyReference; //foreign key look up
rd.ChildTableName = data.TableName;
 
//Get foreign key for column "Number" in foreign table
rd.RelationKeys.Add("Description", "Number");
 
//Setting the Foreign key table in table descriptor
rd.ChildTableDescriptor.VisibleColumns.Add("Word"); //display column
rd.ChildTableDescriptor.SortedColumns.Add("Word");
rd.ChildTableDescriptor.AllowEdit = false; //no editing of foreign table
rd.ChildTableDescriptor.AllowNew = false;  //no new items added to foreign table
rd.ChildTableDescriptor.Appearance.AlternateRecordFieldCell.BackColor = Color.FromArgb(0xff, 0xbf, 0x34);
 
//Adding the relation foreign key descriptor to main table descriptor
td.Relations.Add(rd);
 
//Replace maintable.LookUpColumn with foreigntable.DisplayColumn
string foreignPrefix = rd.Name + "_";
 
//get the hashed name of foreign column.
string foreignDisplayColInMainTable = foreignPrefix + "Word";
td.VisibleColumns.Insert(lookUpIndex, foreignDisplayColInMainTable);

VB

'Creating the Foreign key table
Dim rd As New GridRelationDescriptor()
rd.Name = "ChoiceList" 'just some unique name
rd.RelationKind = RelationKind.ForeignKeyReference 'foreign key look up
rd.ChildTableName = data.TableName
 
'Get foreign key for column "Number" in foreign table
rd.RelationKeys.Add("Description", "Number")
 
'Setting the Foreign key table in table descriptor
rd.ChildTableDescriptor.VisibleColumns.Add("Word") 'display column
rd.ChildTableDescriptor.SortedColumns.Add("Word")
rd.ChildTableDescriptor.AllowEdit = False 'no editing of foreign table
rd.ChildTableDescriptor.AllowNew = False 'no new items added to foreign table
rd.ChildTableDescriptor.Appearance.AlternateRecordFieldCell.BackColor = Color.FromArgb(&Hff, &Hbf, &H34)
 
'Adding the relation foreign key descriptor to main table descriptor
td.Relations.Add(rd)
 
'Replace maintable.LookUpColumn with foreigntable.DisplayColumn
Dim foreignPrefix As String = rd.Name & "_"
 
'get the hashed name of foreign column.
Dim foreignDisplayColInMainTable As String = foreignPrefix & "Word"
td.VisibleColumns.Insert(lookUpIndex, foreignDisplayColInMainTable)

Samples:

C#: SortByDisplayMember_c#.

VB: SortByDisplayMember_VB.

Reference link: https://help.syncfusion.com/windowsforms/gridgrouping/sorting#sort-by-display-member

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon