Articles in this section
Category / Section

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

2 mins read

Sort column by displaymember

The default behavior of the sorting method is relying WinForms Grid Control 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

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied