BoldDeskPremium customer service software with affordable pricing: $10 for 3 agents. Try it for free.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Syncfusion.Windows.Forms.Grid.Grouping;
using Syncfusion.Windows.Forms.Grid;
using Syncfusion.Grouping;
namespace GGC_CustomSort
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private Syncfusion.Windows.Forms.Grid.Grouping.GridGroupingControl gridGroupingControl1;
private System.Windows.Forms.Label label1;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.gridGroupingControl1 = new Syncfusion.Windows.Forms.Grid.Grouping.GridGroupingControl();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.gridGroupingControl1)).BeginInit();
this.SuspendLayout();
//
// gridGroupingControl1
//
this.gridGroupingControl1.BackColor = System.Drawing.SystemColors.Window;
this.gridGroupingControl1.Location = new System.Drawing.Point(32, 48);
this.gridGroupingControl1.Name = "gridGroupingControl1";
this.gridGroupingControl1.Size = new System.Drawing.Size(488, 376);
this.gridGroupingControl1.TabIndex = 0;
this.gridGroupingControl1.Text = "gridGroupingControl1";
this.gridGroupingControl1.VersionInfo = "3.301.0.0";
//
// label1
//
this.label1.Location = new System.Drawing.Point(80, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(400, 23);
this.label1.TabIndex = 1;
this.label1.Text = "Col2 sorts on the value mod 3.";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(552, 454);
this.Controls.Add(this.label1);
this.Controls.Add(this.gridGroupingControl1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.gridGroupingControl1)).EndInit();
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
#region Get the DataSource
DataTable dt = new DataTable("MyTable");
int nCols = 4;
int nRows = 20;
Random r = new Random(123345345);
for(int i = 0; i < nCols; i++)
dt.Columns.Add(new DataColumn(string.Format("Col{0}", i), typeof(int)));
for(int i = 0; i < nRows; ++i)
{
DataRow dr = dt.NewRow();
for(int j = 0; j < nCols; j++)
dr[j] = r.Next(10);
dt.Rows.Add(dr);
}
#endregion
this.gridGroupingControl1.DataSource = dt;
//use this event to hook into the sorting to use custom comparer
this.gridGroupingControl1.TableDescriptor.SortedColumns.Changing += new Syncfusion.Collections.ListPropertyChangedEventHandler(SortedColumns_Changing);
//not needed for the custom sorting, just to display the colored rows
//use event to color row based on [Col2] Mod 2
this.gridGroupingControl1.TableControlPrepareViewStyleInfo += new GridTableControlPrepareViewStyleInfoEventHandler(gridGroupingControl1_TableControlPrepareViewStyleInfo);
}
//used to set up custom comparer
private void SortedColumns_Changing(object sender, Syncfusion.Collections.ListPropertyChangedEventArgs e)
{
SortColumnDescriptor sd = e.Item as SortColumnDescriptor;
if(sd != null && sd.Name == "Col2"
&& e.Action == Syncfusion.Collections.ListPropertyChangedType.Add)
{
sd.Comparer = new CustomComparer();
}
}
//used to color rows based on [Col2] Mod 3
private void gridGroupingControl1_TableControlPrepareViewStyleInfo(object sender, GridTableControlPrepareViewStyleInfoEventArgs e)
{
GridTableCellStyleInfo style = e.Inner.Style as GridTableCellStyleInfo;
if(style.TableCellIdentity.TableCellType == GridTableCellType.RecordFieldCell
|| style.TableCellIdentity.TableCellType == GridTableCellType.AlternateRecordFieldCell)
{
GridRecordRow recRow = style.TableCellIdentity.DisplayElement as GridRecordRow;
if(recRow != null)
{
object o = recRow.ParentRecord.GetValue("Col2");
if( o != null && o.ToString().Length > 0)
{
int modValue = (int)o % 3;
switch((int)o % 3)
{
case 0:
style.BackColor = Color.Red;
break;
case 1:
style.BackColor = Color.Blue;
break;
case 2:
style.BackColor = Color.Green;
break;
default:
break;
}
}
}
}
}
}
public class CustomComparer : IComparer
{
public CustomComparer()
{
}
#region IComparer Members
public int Compare(object x, object y)
{
int c;
if(x == null && y == null)
c = 0;
else if(x == null)
c = -1;
else if(y == null)
c = 1;
else
{
int ix = (int) x % 3;
int iy = (int) y % 3;
if(ix < iy)
c = -1;
else if(ix > iy)
c = 1;
else
{
c = 0;
}
}
return c;
}
#endregion
}
}