Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Datagridview sort not working as expected with combo box cells
 

Datagridview sort not working as expected with combo box cells

I'm using the .Net 3.5 datagridview and am stuck on getting the column sort to work with columns that use combo boxes. An individual combo box displays text values derived from a lookup table. In the data properties for a datagridview column I am defining a DisplayMember and a ValueMember which are both derived from a lookup table. The DisplayMember contains text and the ValueMember is an integer number. The value member is the value stored in the data source that is bound to the datagridview. The sorting is be working with the ValueMember and not the DisplayMember. How do I define the sorting to be on the DisplayMember?

For example, I have a column named 'Mine Name' in the datagridview. In the lookup table assigned to the combobox for this column I have a record for the Olinghouse Mine where field 'MineName' contains the the actual text for the name and field 'DB_MineNameID'contains a unique integer number (1).The datagridview control is bound to a binding source named 'tbl_ProjectDataBindingSource' which is connected to a SQL Server DB table named 'tbl_ProjectData'. The mine ID is stored in 'tbl_ProjectData' as the integer number. Reference to the actual mine name is via a relation between table 'tbl_Projects' and table 'LU_MineNames'. In the datagridview I don't display the integer number but the actual name and I want the sorting to occur on the name rather than the number being stored in the underlying table. How do I do this? Thanks in advance.

Dennis Geasan

Dennis Geasan  Friday, August 21, 2009 6:11 PM
I don't know that this is possible without manually performing your sort on your DataGridView column.


www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Friday, August 21, 2009 9:33 PM
Hi Dennis Geasan,

My test indicate you are totally right. The DataGridView only support to sort value when click the column header. And also DataGridView.SortCompare event occurs only when the DataSource property is not set and the VirtualMode property value is false. So when it is databind, the event won't fired. We cannot hode a hope to use SoreCompare event to do custom sort.

I think that will be difficult to implement sort based on the cell's text instead of value in DataGridView.

Sorry for the inconvenience.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, August 25, 2009 2:45 AM
The help page for DataGridView.Sort Method (DataGridViewColumn, ListSortDirection) states this: "By default, the sort operation will use the Compare method to compare pairs of cells in the column using the DataGridViewCell.Value property." Is there a way to define the sort item to be DataGridViewCell.FormattedValue?

DG
Dennis Geasan  Wednesday, September 02, 2009 11:46 PM
Hi Dennis Geasan,

What I have researched is just like you mentioned. I have handled the SortCompare event to custom the sort action. The only problem is we cannot get the text of a cell, what we can get is just the value of that cell. SortCompare event doesn't offer any parameter to give you FormattedValue information.

So that is the key reason why this cannot implement.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Thursday, September 03, 2009 2:29 AM

You can use google to search for other answers

Custom Search

More Threads

• what is standard way for Save/Retrive Color type into/from windows registry?
• Maintaining shift/ctrl click functionality with drag and drop.
• Freeze Panes For A Dynamically Created Datagrid
• DataGridView cells repaint incorrectly when horizontal scrollbar disappears
• How to determine if DataGridView contains uncommitted changes.
• ArgumentOutOfRangeException when Filling DataTable databound to combobox or listbox
• Binding an IListSource's property to another object's property?
• DataSet Clear() performance problem
• how to add the two (PictureBox,Button) in DataGridView's one same cell(VS2005)
• DataGridViewCheckBoxCell not using Style?