Groups | Blog | Home
all groups > dotnet windows forms databinding > july 2007 >

dotnet windows forms databinding : Sorting in DataGridView



Jack Jackson
7/17/2007 2:31:35 PM
I have a DataGridView bound to a DataTable via a BindingSource object.

Is there a way I can control how the sort comparisons are done when
columns in the DataGridView are clicked?

I can't see where the sorting is done. My guess is in the default
DataView of the DataTable, but DataView doesn't seem to provide any
way to modify the sort comparisons.

I have two reasons for wanting to change the default sorting. First
is the way punctuation is handled (characters like ' and - appear to
be ignored in the sorting, which I do not want), and second is to do
Jack Jackson
7/17/2007 4:05:23 PM
[quoted text, click to view]

That event doesn't fire if the DataGridView's DataSource is set,
presumably because the DataGridView is not doing the sorting in that
case:

"This event occurs only when the DataSource property is not set and
Paul
7/17/2007 4:42:46 PM
[quoted text, click to view]
From help:

private void dataGridView1_SortCompare(object sender,
DataGridViewSortCompareEventArgs e)
{
// Try to sort based on the cells in the current column.
e.SortResult = System.String.Compare(
e.CellValue1.ToString(), e.CellValue2.ToString());

// If the cells are equal, sort based on the ID column.
if (e.SortResult == 0 && e.Column.Name != "ID")
{
e.SortResult = System.String.Compare(
dataGridView1.Rows[e.RowIndex1].Cells["ID"].Value.ToString(),
dataGridView1.Rows[e.RowIndex2].Cells["ID"].Value.ToString());
}
e.Handled = true;
}
Marc Gravell
7/18/2007 12:00:00 AM
In addition to Paul's comment, for automatic support it all depends on
the backing data-source.

Note that DataGridView can perform single-column sorting using the
IBindingList.ApplySort() method; so *if* you control the list, you
could educate it how to sort correctly? By default, it doesn't support
multi-column sorting, but I have a subclass of DataGridView that does
this by using the IBindingListView.ApplySort() method on the backing
data-source (if supported).
I also have a subclass of BindingList<T> that implements this code
simply by using the default compare on the data (via the
PropertyDescriptors). I can post some limited code if needed...

Marc

Jack Jackson
7/18/2007 9:03:15 AM
On Wed, 18 Jul 2007 10:11:25 +0100, "Marc Gravell"
[quoted text, click to view]

I would appreciate seeing your code.

I am surprised that no one else seems bothered by what I and my
co-worders consider to be the oddness of the sort. I checked my phone
book, and while it does ignore single quotes in sorting, it does not
ignore dashes like ADO does.

Even if you think the ADO sorting is OK it presents a problem for our
users. While we transition to ADO, we will still have some programs
that don't use ADO and will sort using SQL Server sort, which means
the users will see data displayed in different orders depending on
which program they run.

Does anyone know where the sorting algorithm is documented? I haven't
Jack Jackson
7/18/2007 6:23:49 PM
On Tue, 17 Jul 2007 14:31:35 -0700, Jack Jackson
[quoted text, click to view]

I've managed to learn a little more about this.

..NET supports three types of String comparisons: Word, String and
Ordinal. Apparently ADO .NET uses Word. Word is a culture-sensitive
comparison "in which certain nonalphanumeric Unicode characters might
have special weights assigned to them".

Just out of curiosity, how does one determine how that
Marc Gravell
7/19/2007 12:00:00 AM
I would *imagine* that it is using the default .NET string sort...

which bits of code do you mean? I don't want to start ripping it all
apart if you don't need it...

the grid? the list?

Marc

Jack Jackson
7/19/2007 8:52:17 AM
On Thu, 19 Jul 2007 09:30:36 +0100, "Marc Gravell"
[quoted text, click to view]

It does, but .NET supports three types of String comparisons: Word,
String and Ordinal. Apparently ADO .NET uses Word. Word is a
culture-sensitive comparison "in which certain nonalphanumeric Unicode
characters might have special weights assigned to them". In en-US it
appears that the Word sort causes some (perhaps all) punctuation
characters to be ignored in comparisons.

[quoted text, click to view]

AddThis Social Bookmark Button