From: Paul on
Hi,

If I associate a DataGridView to a LIST <>, the Grid not sorted by
clicking on the header.
Any idea to do this?

Thanks
From: Siv on
Paul,
Try implementing this:

This code is pinched from one of my projects, the control being sorted is a
listview, it is populated by data from SQL Server but this won't affect your
project as lonbg as when you click the column headings your list contains
data it should work.

Private Sub lstPacks_ColumnClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.ColumnClickEventArgs) Handles lstPacks.ColumnClick
lstPacks.ListViewItemSorter = New ListViewItemComparer(e.Column)
End Sub

' Implements the manual sorting of items by columns.
Class ListViewItemComparer

Implements IComparer

Private col As Integer

Public Sub New()
col = 0
End Sub

Public Sub New(ByVal column As Integer)
col = column
End Sub

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer _
Implements IComparer.Compare
If ScreenUpdating Then
Exit Function
End If
Return [String].Compare(CType(x,
ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
End Function
End Class

The bit about "If ScreenUpdating then : exit function : end if" is there to
stop the Compare routine being called if I am doing a screen update
populating the list as I found that whilst updating the list you don't want
this code active or it causes errors.
I have a variable "ScreeUpdating" which I set to True whilst my screen
update routines are running and then I set back to False at the end of the
update so that functions like this can test if they are being called whilst
an update is occuring and either do or do not respond.


Hopefully this will do what you want.

Siv
--
Martley, Near Worcester, UK


"Paul" wrote:

> Hi,
>
> If I associate a DataGridView to a LIST <>, the Grid not sorted by
> clicking on the header.
> Any idea to do this?
>
> Thanks
> .
>