Home All Groups Group Topic Archive Search About

VB2005 - Sort DataGridView on Text (FormattedValue) of a ComboBox column instead of underlying value

Author
17 Jun 2006 4:15 AM
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

Author
17 Jun 2006 5:47 AM
Cor Ligthert [MVP]
Matt,

I do probably not understand your question, but to sort a datagridview is
typical used a dataview.
The property of that is a text(string) property.

http://msdn2.microsoft.com/en-us/library/system.data.dataview.sort.aspx

Is that what you need?

Cor

Show quoteHide quote
"Matt" <breakthrough@community.nospam> schreef in bericht
news:emWpISckGHA.1208@TK2MSFTNGP02.phx.gbl...
>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
Author
17 Jun 2006 7:52 PM
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

Cor Ligthert [MVP] wrote:
Show quoteHide quote
> Matt,
>
> I do probably not understand your question, but to sort a datagridview is
> typical used a dataview.
> The property of that is a text(string) property.
>
> http://msdn2.microsoft.com/en-us/library/system.data.dataview.sort.aspx
>
> Is that what you need?
>
> Cor
>
> "Matt" <breakthrough@community.nospam> schreef in bericht
> news:emWpISckGHA.1208@TK2MSFTNGP02.phx.gbl...
>> 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
>
>
Author
18 Jun 2006 6:41 AM
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


Show quoteHide quote
"Matt" <breakthrough@community.nospam> schreef in bericht
news:%23s%23JhdkkGHA.1640@TK2MSFTNGP02.phx.gbl...
> 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
>
> Cor Ligthert [MVP] wrote:
>> Matt,
>>
>> I do probably not understand your question, but to sort a datagridview is
>> typical used a dataview.
>> The property of that is a text(string) property.
>>
>> http://msdn2.microsoft.com/en-us/library/system.data.dataview.sort.aspx
>>
>> Is that what you need?
>>
>> Cor
>>
>> "Matt" <breakthrough@community.nospam> schreef in bericht
>> news:emWpISckGHA.1208@TK2MSFTNGP02.phx.gbl...
>>> 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
>>
Author
23 Jun 2006 11:21 AM
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.
====================================================