Sort a DataGridView by DisplayMember

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2010-05-24T09:05:34Z Indexed on 2010/05/24 9:11 UTC
Read the original article Hit count: 134

Filed under:

Hi,

I have a DataGridView that is bound to a DataTable. In this table there are some foreign keys. I am then using the CellFormatting event to get the corresponding text from another database table for each foreign key.

I want to sort the DataGridView when the user clicks the header. Automatic sorting works but is not correct as it is Sorting on the ValueMember (ForeignKey ID) and not on the DisplayMember (the text).

I tried using the SortCompare event but then I read that it does not work on DataGridViews that use the DataSource property.

How can this be done?

Thanks

© Stack Overflow or respective owner

Related posts about winforms