Customized sorting on DataTable in C#?
- by Steve
This is a C# Winform question. I have a DataGridView which is bounded to a DataTable. I construct the DataTable myself, which several DataColumn instances. When the DataTable is bound to the DataGridView, by default, every column is sortable by clicking the headers of the DataGridView.
But the sorting behavior is something "by default". It seems that it is sorted by string. This is true even if I put this as my code:
DataColumn dc = new DataColumn("MyObjectColumn", typeof(MyObject));
And MyObject has overriden ToString() and has implemented the IComparable interface. That means even if I have told the DataTable how to sort the special column with the implementation of IComparable interface, DataGridView still doesn't do it the way I expect.
So how can I let DataTable sort data in the way I want?
Thanks for the answers.