I have a column in my datagridview that is a combobox column. I would like it sorted by it's display value (i.e. text) instead of it's value (in this case, a list of int's from the database).
How can I do this?
I have a column in my datagridview that is a combobox column. I would like it sorted by it's display value (i.e. text) instead of it's value (in this case, a list of int's from the database).
How can I do this?
I would take a look at How to: Customize Sorting in the Windows Forms DataGridView Control. There are three different ways to sort in that article. A couple of other posts that might help out as well: How to sort databound datagridview and Custom sort of datagridview
Apparently, this is not possible. I was looking for the same thing and I am not sure why no one would have thought this was an important feature. Here is the link to the original question and the answer from a Microsoft Rep.
Hi Matt,
Thank you for posting.
If you have a DataGridViewComboBoxColumn in your DataGridView, you have to set the DataSource of the DataGridView to populate data in it. That's to say, you must bind the DataGridView to a datasource.
If you set the SortMode of the DataGridViewComoboBoxColumn to Automatic, you can sort this column by clicking it. The sorting is based on the cells' value in the column, not the FormattedValue. If you want to sort this column based on the FormattedValue, it seems that you have to customize the sorting.
There're several options for custom sort of DataGridView. These options are listed below. 1. Calling DataGridView.Sort(DataGridViewColumn,ListSortDirection) method 2. Handling with the DataGridView's SortCompare event 3. Calling DataGridView.Sort(IComparer) method
You can refer to the below link for more information on the above three options: http://msdn2.microsoft.com/en-us/library/ms171608(d=ide).aspx
- Using DataView and set the its Sort property
You can refer to he below link for more information on the option4: http://msdn2.microsoft.com/en-us/library/system.data.dataview.sort(d=ide).as px
It seems that only option2 and option3 could be applied to sort the DataGridViewComboBoxColumn based on the FormattedValue. However, these two options only apply under the condition that the DataGridView is NOT bounded to a DataSource.
So in my opinion, it is impossible to sort a DataGridViewComboBoxColumn based on the FormattedValue.
If you have other concerns, please don't hesitate to let me know.
Sincerely, Linda Liu Microsoft Online Community Support