How to create custom group column
I have a datatable. I would like to create a list of group columns according to my order (not alphabetical as the default), then arrange the rows of the datatable into the datagrid groups according to a criteria in one of the columns in the datatable.
For instance, my datatable is
id name country
1 Joe South Africa
2 Ashley England
3 James Peru
4 Mike Spain
5 Phil Brazil
I want the datagrid like this
Id Name
EUROPE ===> Group column
2 Ashley
4 Mike
AFRICA ===> Group column
1 Joe
SOUTH AMERICA ===> Group column
3 James
5 Phil
The datatable is now grouped into continents into the datagrid.
How do I achieve this?
Thank you for contacting Syncfusion Support.
Your requirement can be achieved by specify the custom logic through GroupColumnDescription.KeySelector property and the column name to GroupColumnDescription.ColumnName property in SfDataGrid. For more information, please refer the below user guide documentation link,
UG Link: https://help.syncfusion.com/windowsforms/datagrid/grouping#custom-grouping
Please let us know if you have any concerns in this.
Regards,
Vijayarasan S
Yes, I've seen it before. The issue is that I don't understand the example neither do I know how to apply it to may own case. I was hoping you will use my own case to make an example for me.
Thanks for the update.
Your requirement can be achieved by maintain the country names in different list related to continent and customize the KeySelector in SfDataGrid.GroupColumnDescription. Please refer the below code snippet,
|
List<string> EuropeContinent;
List<string> AfricaContinent;
List<string> SouthAmericaContinent;
public Form1()
{
InitializeComponent();
EuropeContinent = new List<string>();
EuropeContinent.Add("England");
EuropeContinent.Add("Spain");
AfricaContinent = new List<string>();
AfricaContinent.Add("South Africa");
SouthAmericaContinent = new List<string>();
SouthAmericaContinent.Add("Peru");
SouthAmericaContinent.Add("Brazil");
var table = this.GetDataTable();
sfDataGrid1.DataSource = table;
sfDataGrid1.GroupCaptionTextFormat = "{Key}";
//Apply the CustomGrouping for DateColumn by using KeySelector.
this.sfDataGrid1.GroupColumnDescriptions.Add(new GroupColumnDescription()
{
ColumnName = "Country",
KeySelector = (string ColumnName, object o) =>
{
var item = (o as DataRowView).Row["Country"];
if (EuropeContinent.Contains(item.ToString()))
return "EUROPE";
if (AfricaContinent.Contains(item.ToString()))
return "AFRICA";
if (SouthAmericaContinent.Contains(item.ToString()))
return "SOUTH AMERICA";
return "Country Name mismatched";
}
});
sfDataGrid1.ExpandAllGroup();
} |
Stack Overflow Link: https://stackoverflow.com/questions/5450328/get-a-countrys-continent-in-c-sharp
Regards,
Vijayarasan S
Great 👍. However, in your sample, the group column starts with Africa contrary to my initial example that starts with Europe.
Any workaround for this?
Thanks for the update.
Your requirement can be achieved by applying the custom sorting in SfDataGrid. Please refer the below code snippet,
|
//customsorting applied for Country column
this.sfDataGrid1.SortComparers.Add(new SortComparer() { Comparer = new CustomComparer(), PropertyName = "Country" });
public class CustomComparer : IComparer<object>, ISortDirection
{
public int Compare(object x, object y)
{
string nameX = string.Empty;
string nameY = string.Empty;
if (x.GetType() == typeof(Group))
{
//Get the group key value
nameX = ((Group)x).Key.ToString();
nameY = ((Group)y).Key.ToString();
}
//Here customized based on your scenario
//returns the comparison result based in SortDirection.
if (nameX != "EUROPE" && nameY == "EUROPE")
return SortDirection == ListSortDirection.Ascending ? 1 : -1;
else if (nameX == "AFRICA")
return SortDirection == ListSortDirection.Ascending ? -1 : 1;
else
return 0;
}
private ListSortDirection _SortDirection;
/// <summary>
/// Gets or sets the property that denotes the sort direction.
/// </summary>
/// <remarks>
/// SortDirection gets updated only when sorting the groups. For other cases, SortDirection is always ascending.
/// </remarks>
public ListSortDirection SortDirection
{
get { return _SortDirection; }
set { _SortDirection = value; }
}
} |
For more information related to Custom Sorting, please refer the user guide documentation,
Please let us know if you have any concerns in this.
Works as requested but I don't seam to understand this part neither could I apply it to my real project.
Thanks for the update.
nameX contains the Key value of FirstRecordGrouped and nameY contains the key value of SecondRecordGrouped. Please refer the below screen shot,
Please let us know if you have any concerns in this.
Regards,
Vijayarasan S
Thanks. I had to later study the incomparable interface to really understand what was going on and I've been able to apply it to my real project.
Another issue I have is that I want to reorder the rows through the mouse drag n drop. I could only found an article for that in wpf and not winform. Can I also get a help?🤨
Thanks for the update.
Currently, SfDataGrid does not have a support for the row drag and drop. We have analyzed and considered your requirement of “Provide the row drag and drop support” in SfDataGrid and logged feature request for the same. We will implement this feature in any of our upcoming release.
We have included the "Row drag and drop" feature in our our Essential Studio® 2025 Volume 4 Main Release v32.1.19 and is available for download under the following link.
Please refer to below UG
https://help.syncfusion.com/windowsforms/datagrid/draganddrop#row-drag-and-drop
- 10 Replies
- 3 Participants
- Marked answer
-
OL Olayinka
- Aug 21, 2021 09:13 PM UTC
- Dec 17, 2025 11:44 AM UTC