Mike Cluck
Mike Cluck

Reputation: 32511

Sorting DataGridView by Column.DisplayMember

I have a DataGridView with a few DataGridViewComboBoxColumn's where the actual value is tied to an ID but the DisplayMember is the string counterpart in a lookup table. I'm trying to make it so when I sort by that column then the sort is done based on the DisplayMember, not the ValueMember. I know this was addressed in this question but the answer was less than in depth nor did I understand it.

What I've tried thus far

Edit: To further clarify: I'm attempting to sort the entire DataGridView based on the DisplayMember of the comboboxes, not sort the comboboxes themselves.

How can I sort a DataGridView based on the DisplayMember of a databound DataGridViewComboBoxColumn?

Upvotes: 2

Views: 3980

Answers (1)

TarkaDaal
TarkaDaal

Reputation: 19585

Your third attempt is almost right: What you need to do is create an extra column in your DataSet to store the value of the DisplayMember. Then, you create an invisible bound DataGridViewColumn, and bind it to this extra DataSet column. Then it's a bound column, and you can programmatically sort on it.

The question you've linked to is doing the same, only that solution is generating the display member text in SQL, before it's returned to the application.

You'd think this'd be simple, wouldn't you? :)

Upvotes: 2

Related Questions