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?


9 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team August 23, 2021 01:52 PM UTC

Hi Olayinka,

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 



OL Olayinka replied to Vijayarasan Sivanandham August 23, 2021 02:06 PM UTC

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.



VS Vijayarasan Sivanandham Syncfusion Team August 24, 2021 02:44 PM UTC

Hi Olayinka,

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(); 
} 
Please refer the below Stack overflow link for more details,

Stack Overflow Link: https://stackoverflow.com/questions/5450328/get-a-countrys-continent-in-c-sharp 
Please let us know if you have any concerns in this.

Regards,
Vijayarasan S 



OL Olayinka replied to Vijayarasan Sivanandham August 25, 2021 01:13 PM UTC

Great 👍. However, in your sample, the group column starts with Africa contrary to my initial example that starts with Europe.

​Any workaround for this?



VS Vijayarasan Sivanandham Syncfusion Team August 26, 2021 07:43 AM UTC

Hi Olayinka,

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. 

Regards, 
Vijayarasan S 


Marked as answer

OL Olayinka replied to Vijayarasan Sivanandham August 27, 2021 10:16 PM UTC

Works as requested but I don't seam to understand this part neither could I apply it to my real project.

if (nameX != "EUROPE" && nameY == "EUROPE") 
                return SortDirection == ListSortDirection.Ascending ? 1 : -1; 
            else if (nameX == "AFRICA") 
                return SortDirection == ListSortDirection.Ascending ? -1 : 1; 
            else 
                return 0; 
        } 

Can please please explain what's happening in the code. Which one is nameX and nameY? What if another column group is added, how how will the code look like?
I just can't wrap my head around this. Please, help.


VS Vijayarasan Sivanandham Syncfusion Team August 30, 2021 01:58 PM UTC

Hi Olayinka,

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, 
 
 
 
Country property is compared based on string comparison value based on nameX and nameY. Firstcondition satisfied nameY(SecondRecordGrouped) moved into before the nameX(FirstRecordGrouped). Please refer the below screen shot,
 
Video Link: https://www.syncfusion.com/downloads/support/forum/168262/ze/CustomSorting1008858834

Please let us know if you have any concerns in this.

Regards,
Vijayarasan S 



OL Olayinka September 3, 2021 05:01 PM UTC

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?🤨



VS Vijayarasan Sivanandham Syncfusion Team September 6, 2021 12:21 PM UTC

Hi Olayinka,

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.
 
 
At the planning stage for every release cycle, we review all open features and identify features for implementation based on specific parameters including product vision, technological feasibility, and customer interest. We will let you know when this feature is implemented. We appreciate your patience until then. 
 
Thank you for requesting this feature and helping us define it. We are always trying to make our products better and feature requests like yours are a key part of our product growth efforts.  
 
 
If you have any more specification/suggestions to the feature request, you can add it as a comment in the portal and cast your vote to make it count.  
 
Regards, 
Vijayarasan S   


Loader.
Up arrow icon