Reputation: 32511
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
SortCompare
event but discovered it isn't fired on a databound column.ColumnHeaderMouseClick
event but rows in a DataGridViewRowCollection
are read-only and I can't programatically insert rows (while swapping) on a databound collection.DataGridViewTextBoxColumn
where the cells are automatically set to the DisplayMember
of the original column then attempting to sort that column instead. However, a databound collection cannot be sorted based on an unbounded column.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
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