DataGridView - SortCompare event not firing!
-
Hi all, I have a Winforms 2.0 app in which I have a DataGridView which is populated from my own data objects (ie: from my own classes, not from a database or strongly typed dataset). The DGV is readonly so no editing or adding new rows can be done. Since some of the columns need to have a custom sort I set the SortMode property on those columns to "Programmatic" and then added the following event handler to the code: Private Sub dgvMeetings_SortCompare(ByVal sender As System.Object, ByVal e As DataGridViewSortCompareEventArgs) Handles dgvMeetings.SortCompare 'Try to sort based on the columns in the current column. e.SortResult = System.String.Compare(e.CellValue1.ToString(), e.CellValue2.ToString()) 'If the cells are equal, sort based on the race start time column If e.SortResult = 0 Then e.SortResult = System.String.Compare(dgvMeetings.Rows(e.RowIndex1).Cells("RaceDate").Value.ToString(), _ dgvMeetings.Rows(e.RowIndex2).Cells("RaceDate").Value.ToString()) End If e.Handled = True End Sub However, this event is simply not firing at all... I can click on the column headers until I'm blue in the face and the event never fires. The columns in the DGV that are set to sort automatically work fine so would anyone know why SortCompare is not working? I also tried adding an AddHandler which pointed to the same method (without the "Handles ..." of course) and it still ignores it. I actually got the code above from the MS document about the DGV (at http://download.microsoft.com/download/5/6/4/5646742C-3EB7-48F7-BFB3-CC295D618CF9/DataGridView FAQ.doc) in which it says this is the way to custom sort unbound data - but it doesn't seem to work for me... anyone know what I might be missing here? TIA for any help... Mike -- modified at 0:13 Sunday 7th May, 2006
-
Hi all, I have a Winforms 2.0 app in which I have a DataGridView which is populated from my own data objects (ie: from my own classes, not from a database or strongly typed dataset). The DGV is readonly so no editing or adding new rows can be done. Since some of the columns need to have a custom sort I set the SortMode property on those columns to "Programmatic" and then added the following event handler to the code: Private Sub dgvMeetings_SortCompare(ByVal sender As System.Object, ByVal e As DataGridViewSortCompareEventArgs) Handles dgvMeetings.SortCompare 'Try to sort based on the columns in the current column. e.SortResult = System.String.Compare(e.CellValue1.ToString(), e.CellValue2.ToString()) 'If the cells are equal, sort based on the race start time column If e.SortResult = 0 Then e.SortResult = System.String.Compare(dgvMeetings.Rows(e.RowIndex1).Cells("RaceDate").Value.ToString(), _ dgvMeetings.Rows(e.RowIndex2).Cells("RaceDate").Value.ToString()) End If e.Handled = True End Sub However, this event is simply not firing at all... I can click on the column headers until I'm blue in the face and the event never fires. The columns in the DGV that are set to sort automatically work fine so would anyone know why SortCompare is not working? I also tried adding an AddHandler which pointed to the same method (without the "Handles ..." of course) and it still ignores it. I actually got the code above from the MS document about the DGV (at http://download.microsoft.com/download/5/6/4/5646742C-3EB7-48F7-BFB3-CC295D618CF9/DataGridView FAQ.doc) in which it says this is the way to custom sort unbound data - but it doesn't seem to work for me... anyone know what I might be missing here? TIA for any help... Mike -- modified at 0:13 Sunday 7th May, 2006
Worked out what is was - I actually needed to set the sort mode of each column to "Automatic" rather than "Programmatic". Seems the latter mode is really only for managing the sort glyphs etc. Now the SortCompare event fires but I have a new problem which is to get numeric columns such as "Race Number" to sort by the value (not the string value) then by the race start date (which is a DateTime column). I was going to delete this post but thought I'd leave it there for anyone else that runs into the same problem. Mike