Sorting and Filtering are very frequently used database operations while working with Grid controls. C1DataGrid for Silverlight supports both of  these  interactive operations. However, none of its operations are  performed at Grid level. Both Sorting and Filerting are done at DataSource. C1DataGrid does not have any internal logic for these operations.

But there could be a scenario where the user might require to view the data in a specific manner. Consider an example where we have the following data in the DataSource.

aaa
BBB
AAA
bbb
XYZ
xz
jjk

If we do default sorting on this set of data, grid will display the data in the following manner

Ascending     Descending
aaa                  xz
AAA               XYZ
bbb                 jjk
BBB               BBB
jjk                   bbb
XYZ               AAA
xz                   aaa

Whereas the customer may wish to see the data where all the words starting lowercase characters are clubbed together and then the words starting with uppercase characters.

But C1DataGrid does not have the option to customize the sorting on its own. So what can we do? Here comes Microsoft to our rescue. Using its .Net defined Interfaces we can implement our custom sorting. .Net provides the option of IComparer interface which we can leverage upon to define our own sorting logic on the DataSource.

So lets start with the implementation.

1. We start by defining a Class structure used for binding

 public class MyDataClass
{
public MyDataClass() { }

public MyDataClass(string nm)
{
Name = nm;
}

public string Name { get; set; }
}


2. Next step involves defining Sorting Logic using IComparere interface.  We will need to define two Classes for Ascending and   descending sort.

class MyDataClass_SortByCaseSensitivityASc : IComparer<MyDataClass>
{
public int Compare(MyDataClass x, MyDataClass y)
{
int valX = (int)x.Name[0];
int valY = (int)y.Name[0];

if (valX > valY)
return 1;
else if (valX < valY)
return -1;
else
return 0;
}
}

class MyDataClass_SortByCaseSensitivityDesc : IComparer<MyDataClass>
{
public int Compare(MyDataClass x, MyDataClass y)
{
int valX = (int)x.Name[0];
int valY = (int)y.Name[0];

if (valX < valY)
return 1;
else if (valX > valY)
return -1;
else
return 0;
}
}


3. To define the sorting logic, we need to handle the sorting event for the grid where we will apply the Sorting on the actual datasource and rebind the C1DataGrid ItemSource.

// variable used to mark the ascending or descending
// state of the grid
int flag = -1;

void c1DataGrid1_SortChanging(object sender, C1.Silverlight.DataGrid.DataGridSortChangingEventArgs e)
{
e.Cancel = true;

if (e.ChangingColumns[0].Column.Name == "Name")
{
//Ascending Order
if (flag == -1 || flag == 1)
{
c1DataGrid1.ItemsSource = null;
MyDataClass_SortByCaseSensitivityASc mAsc = new MyDataClass_SortByCaseSensitivityASc();
myList.Sort(mAsc);
c1DataGrid1.ItemsSource = myList;
flag = 0;
}
//Descending Order
else
{
c1DataGrid1.ItemsSource = null;
MyDataClass_SortByCaseSensitivityDesc mDesc = new MyDataClass_SortByCaseSensitivityDesc();
myList.Sort(mDesc);
c1DataGrid1.ItemsSource = myList;
flag = 1;
}
}
}


Once the above code is implemented, sorting the Grid will display the data in the given order.

Ascending   Descending
AAA             xz
BBB             jjk
XYZ             bbb
aaa                aaa
bbb               XYZ
jjk                 BBB
xz                 AAA

Sorting logic mentioned above is just an example. You can do much more on this. You may define any logic based on any of the Class DataMembers and implement the sorting on a different column. If you wish to discover more on IComparer class you can visit the given MSDN link.

http://msdn.microsoft.com/en-us/library/8ehhxeaf%28v=vs.100%29.aspx#Y0

Download Sample