Custom DataGridView column sort based on Value not Formatted value

I have a custom DataGridView cell I'm using to display the contents of MyType objects. To control how they're being formatted I'm overriding the GetFormattedValue() and FormattedvalueType methods of DataGridViewTextBoxCell because in this case I don't want to use the default ToString() method. The problem is that when I do this the DataGridView is sorting the column by the string in FormattedValue instead of by Value.

I'm not seeing a method I can override to change the sort behavior of the column. While I know I can, I don't want to have to write custom DataGridView.Sort() methods because I'm using MyType columns in multiple DataGridViews.

Upvotes: 0

Views: 2714

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292555

Are you filling the DGV manually, or using databinding ?

In the former case, you can handle the SortCompare event, as explained in the link posted by SwDevMan81.

In the latter case, make sure your MyType class implements IComparable. This implementation will be used by the DGV to sort the data.

Upvotes: 1

Related Questions