Sorting of DataGridViewComboBoxColumn type columns

N

nvx

Hi,
is it possible to sort DataGridViewComboBoxColumn type columns by
clicking on the header cell (in DataGridViewColumnSortMode.Automatic
case)? It doesn't seem to work or there is a bug in my code. There
should be an arrow sign (triangle) in the header cell after clicking on
it, but there doesn't appear any...

Thanks for any help.

With regards
nvx
 
K

Kevin Spencer

Yes, it's possible, but I don't know why you're having a problem.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 
N

nvx

Thank you for the information. I'll try to go through the code and find
the problem.

One more thing: is there any other property of
DataGridViewComboBoxColumn columns that HAS to be set to some value for
this to work (except the DataGridViewColumnSortMode.Automatic; I'm
generating the DataGridViewComboBoxColumn columns on the fly...)?

Many thanks...

With regards
nvx
 
N

nvx

Thank you very much, I'll read the document. It looks very useful.

Yes, DataSource for the ComboBox items is an ArrayList of Strings.
SortMode is set to Automatic when I programatically add the
DataGridViewComboBoxColumn column into the DataGridView.

With regards
nvx
 
K

Kevin Spencer

I meant, are you using a DataSource for the DataGridView columns?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 
N

nvx

I'm sorry, I must have misunderstood.

Yes, the DataGridView is bound to an Access database table. After the
grid is filled with data, user can add or remove
DataGridViewComboBoxColumn columns.

Kevin Spencer napsal(a):
 
N

nvx

Well, it seems I didn't have set the DataPropertyName correctly. Due to
certain things one letter was in lowercase and everything went wrong.

Uff, I'm glad it's working... :)

Kevin, thank you very much for your help.

With regards
nvx


nvx napsal(a):
 
W

whittetinternet

I also have a problem doing this. It doesn't seem to work out of the
box and I'm not sure which method is the best to choose to solve it. I
have the DataGridView FAQ and it is great, but in this case it appears
that having a DataSource assigned is hurting my options of sorting on a
field not assigned to the DataSource's DataTable.

I have a DataGridView bound to a DataTable. I then add a
DataGridViewComboBoxColumn and this all works. Now when I click this
column the sort apprears to sort on the ID instead of the NAME
displayed in the combo box.
I have
cbo.DataPropertyName = "SalesUnitID"
cbo.DisplayMember = "NAME"; //Name from the SalesUnit table.
cbo.ValueMember = "ID"; //ID from the SalesUnit table.

--The e-mail is trash, post your response please:)
 
W

whittetinternet

I also have a problem doing this. It doesn't seem to work out of the
box and I'm not sure which method is the best to choose to solve it. I
have the DataGridView FAQ and it is great, but in this case it appears
that having a DataSource assigned is hurting my options of sorting on a
field not assigned to the DataSource's DataTable.

I have a DataGridView bound to a DataTable. I then add a
DataGridViewComboBoxColumn and this all works. Now when I click this
column the sort apprears to sort on the ID instead of the NAME
displayed in the combo box.
I have
cbo.DataPropertyName = "SalesUnitID"
cbo.DisplayMember = "NAME"; //Name from the SalesUnit table.
cbo.ValueMember = "ID"; //ID from the SalesUnit table.

--The e-mail is trash, post your response please:)
 
W

whittetinternet

Here is what I have now:
dataView.Sort = dataGridView.SortedColumn.Name + " " +
(direction == ListSortDirection.Ascending ?
"ASC " : "DESC ");

Do I need to databind to a dataView with the sort?

Right now I am using a single DataTable for the binding to the grid and
a second dataTable for the Sales Unit combobox's DataSource.

So the following only sorts on the ID. Would the solution be to put
both DataTables in a DataSet and then sort on the view with the column
being sorted being "Parent(SalesUnit).Name"?
 
K

Kevin Spencer

You'll have to forgive me. It's late, and I've put in a long day (in a long
series of long days!). In addition, I haven't worked on this particular
project for awhile, and the DataGridView is one complex critter, so I may be
a bit lax in my recollection. But I do want to help, so I'll try to point
you in the right direction.

I believe you're on the right track. As I recall, it is necessary to use
parent/child relationships, and possibly a combination of two related
DataTables to do this, perhaps even a custom View from the DataSet. I know
that's not much, but I'm afraid it's all I have time for right now. In fact,
I should probably go to bed!

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Sequence, Selection, ZZZZZZZZzzzzzzzzzzzzzzzzzzzzzzzz.......................
 
W

whittetinternet

Happy to receive the help.

The solution with a databound grid that I used was to add an
DataColumn.Expression to the combo box column and use that column to
sort using the click event of the DataGridView column header and set
the dataView.Sort = "MyColumnNAME ASC";

Also, we had a request to display an inserted row in Rows[0] after each
insert. This solution will help solve that too.
 

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