Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > VS2008 VB Sorting in Datagridview - time
 

VS2008 VB Sorting in Datagridview - time

In my datagridview I have a column showing a time. The column is formatted "t"
The underlying database-field is a DateTime.
When I sort the column, it doesn't sort correct, as it sorts on the date and then the time - how do I get this problem 'sorted out' ?

mvh ravnø
ravnø  Wednesday, August 26, 2009 5:44 PM
I was not able to find an example of *exactly* what you want to do, but this should get you started:

http://support.microsoft.com/kb/971369

In the example, the default sort did not work because the column was a combobox. But you should be able to do something similar.

Check out especially the ColumnHeaderMouseClick event.

Hope this helps.
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  Wednesday, August 26, 2009 7:23 PM
Hi ravnø,

I think you can customize the sorting action with your own logical by handling SortCompare event. Once you use that event, please make sure you have not use databinding and VirtualMode property value is false.

Here is the document of SortCompare event of DataGridView. I don't know what logical you use to sort the datetime column. So please look at the following sample first. If you need any further help, please feel free to tell me.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.sortcompare.aspx

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Friday, August 28, 2009 3:51 AM

Hi DeborahK

I have looked at your proposal and have tried to add an extra column to my datagridview to sort by. My problem is now to have this column hold only the "hh:MM"-part of the DateTime-field.
Something like newColumn=Format(DateTimeColumn,"hh:MM")
I have also tried to get this function in the Dataset, by adding a new datacolumn and then set the datavalue thru 'expression' - no luck so far.

Another way maybe could be to let the MS SQL database deliver only the time-part ?

mvh ravnø

ravnø  Sunday, September 13, 2009 3:24 PM

Hi Kira

I have to use databinding, as the user must be able to change the time in the dataset / database.

So I don't think, I can use the SortCompare event

mvh ravnø

ravnø  Sunday, September 13, 2009 3:29 PM

You can use google to search for other answers

Custom Search

More Threads

• Deleted row information cannot be accessed through the row.
• DataGridViewColumn
• how do it remove an item from a checkedListBox?
• Refreshing a Listbox
• Making a Copy of a record in the same bindingsource
• Strange problem with DataGridViewComboBoxCell
• How does Generated Insert and Generated TableAdapters work
• Could not bind to the new display member.\r\nParameter name: newDisplayMember
• Problem in Populating datagridview with designed columns
• adding new rows to already bound datagrid in frameowrk 1.1