VB2005 - Sort DataGridView on Text (FormattedValue) of a ComboBoxcolumn instead of underlying value?

M

Matt

I have been searching all over the web for a way to sort a DataGridView based on
the actual text being shown in a ComboBox column as opposed to the underlying
value (an ID in this case). Can anyone tell me if this is even possible, and if
so, how to do it? If this is completely impossible, how would you suggest going
about sorting a ComboBox wherein the text displayed in the column is the
client's name, and the underlying value is an ID?

Thanks,
Matt
 
M

Matt

Cor,

Unfortunately, this does not solve my problem, since it still leaves me unable
to sort by the actual text being displayed in the field, since that text is not
a part of the base DataTable object (it is being referenced in through another
DataSource on the ComboBox Column). What I need is a way to sort on what's
*actually showing* in the ComboBox Column, as opposed to any data that underlies
it. Is this possible?

Thanks,
Matt
 
C

Cor Ligthert [MVP]

Matt,

In my idea is the column that is related to the combobox always a column in
an underlaying table. The combobox is only used to fill it with values.

Why can you not sort on that using the dataview, did you try it already?

Be aware that the sort never standard is working while updating a row in a
datagrid(view).

I hope this helps,

Cor
 
L

Linda Liu [MSFT]

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

4. 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

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top