Public Class frmDGVSwap3
Private Class data
Public Property Daten As String
Public Property Numeric As Decimal
Public Property RowPos As Integer
Public Property ForeColor As Color
Public Property RowHeight As Integer
End Class
Private Class rowcomparer
Implements Collections.Generic.IComparer(Of data)
Private _sortindex As Integer
Public Property sortindex As Integer
Get
Return _sortindex
End Get
Set(value As Integer)
_sortindex = value
End Set
End Property
Public Function Compare(x As data, y As data) As Integer _
Implements System.Collections.Generic.IComparer(Of data).Compare
'Vergleich von zwei Data-Instanzen gemäß der gegebenen Spalte
Dim o As Integer = 1
If _sortindex = 0 Then
If x.Daten < y.Daten Then o = -1
If x.Daten = y.Daten Then o = 0
ElseIf _sortindex = 1 Then
If x.Numeric < y.Numeric Then o = -1
If x.Numeric = y.Numeric Then o = 0
Else
'sonst stets nach RowPos sortieren
If x.RowPos < y.RowPos Then o = -1
If x.RowPos = y.RowPos Then o = 0
End If
Return o
End Function
End Class
'Liste, Bindung und Control
Dim lst As New List(Of data)
Dim bs As new BindingSource
Dim WithEvents dgv As New DataGridView _
With {.Parent = Me, .DataSource = bs}
Private Sub frmDGVSwap_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
'Testdaten
Dim c As Color, h As Integer
For i As Integer = 0 To 99
If i Mod 3 = 0 Then c = Color.Red : h = 20
If i Mod 3 = 1 Then c = Color.Blue : h = 25
If i Mod 3 = 2 Then c = Color.Magenta : h = 30
lst.Add(New data With {.Daten = "Satz " & Format(i, "00"), _
.Numeric = CDec(Math.Round(Rnd() * 100, 2)), _
.RowPos = i, _
.ForeColor = c, .RowHeight = h})
Next i
bs.DataSource = lst
End Sub
Private Sub dgv_CellPainting(sender As Object, _
e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) _
Handles dgv.CellPainting
If e.RowIndex = -1 Or e.ColumnIndex = -1 Then Exit Sub
Dim dt As data = DirectCast(bs(e.RowIndex), data)
With dgv.Rows(e.RowIndex)
.Cells(e.ColumnIndex).Style.ForeColor = dt.ForeColor
.Height = dt.RowHeight
End With
End Sub
Private Sub dgv_ColumnHeaderMouseClick(sender As Object, _
e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
Handles dgv.ColumnHeaderMouseClick
If e.ColumnIndex = -1 Then Exit Sub
'Instanz des Comparers
Dim rc As New rowcomparer
'Festlegen der Spalte fürs Sortieren
rc.sortindex = e.ColumnIndex
lst.Sort(rc)
'Bindung aktualisieren
bs.ResetBindings(False)
End Sub
Dim swapindex As Integer = -1
Private Sub dgv_RowHeaderMouseClick(sender As Object, _
e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
Handles dgv.RowHeaderMouseClick
If e.RowIndex = -1 Then
If Not swapindex = -1 Then
dgv.InvalidateRow(swapindex)
swapindex = -1
End If
Exit Sub
End If
If e.Button = MouseButtons.Right Then
If swapindex = -1 Then
'Erster Click Index merken
swapindex = e.RowIndex
dgv.Rows(swapindex).DefaultCellStyle.BackColor = _
Color.LightGreen
ElseIf swapindex = e.RowIndex Then
'Aufheben bei erneutem Click
dgv.InvalidateRow(swapindex)
swapindex = -1
Else
'Referenzen auf Datensätze besorgen
Dim dt1 As data = CType(bs(e.RowIndex), data)
Dim dt2 As data = CType(bs(swapindex), data)
bs.RemoveAt(e.RowIndex)
bs.Insert(e.RowIndex, dt2)
bs.RemoveAt(swapindex)
bs.Insert(swapindex, dt1)
dgv.InvalidateRow(swapindex)
dgv.InvalidateRow(e.RowIndex)
swapindex = -1
End If
End If
End Sub
End Class |