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?
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.
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();
} |
Great 👍. However, in your sample, the group column starts with Africa contrary to my initial example that starts with Europe.
Any workaround for this?
//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; }
}
} |
Works as requested but I don't seam to understand this part neither could I apply it to my real project.
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?🤨