Datenbindung der ComboBox in .NET
Hier wird beschrieben, in welcher Reihenfolge man am besten die Eigenschaften für eine Combobox oder Listbox setzt, um möglichst wenige und die richtigen Events beim Initialisieren aufzurufen: Erst DisplayMember, dann ValueMember und zum Schluss die DataSource.
The Code Project - Best Practice for Binding WinForms ListControls - C# Programming
Will man die Ausgabe von DisplayMember irgendwie formatieren (bei mir z.B. das Datum), dann muss man DrawMode auf OwnerDrawFixed setzen und das DrawItem-Event wie folgt abfangen:
Private Sub cmbMonat_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cmbMonat.DrawItem
Dim g As Graphics = e.Graphics
Dim drv As DataRowView
Dim dtm As Date
Dim s As String
Dim brText As Brush
Dim rDraw As Rectangle
Dim bSelected As Boolean = CBool(e.State And DrawItemState.Selected)
Dim bValue As Boolean = CBool(e.State And DrawItemState.ComboBoxEdit)
rDraw = e.Bounds
If bSelected And Not bValue Then
brText = SystemBrushes.HighlightText
g.FillRectangle(SystemBrushes.Highlight, rDraw)
Else
brText = New Drawing.SolidBrush(cmbMonat.ForeColor)
g.FillRectangle(New Drawing.SolidBrush(cmbMonat.BackColor), e.Bounds)
End If
rDraw = Nothing
If e.Index = -1 Then Exit Sub
Try
drv = DirectCast(CType(sender, ComboBox).Items.Item(e.Index), DataRowView)
'Hier wird formatiert
dtm = Date.Parse(drv.Item(CType(sender, ComboBox).DisplayMember).ToString)
s = dtm.ToString("MMM yyyy")
Catch ex As Exception
Trace.WriteLine(ex.ToString)
End Try
Dim x, y As Integer
x = e.Bounds.Left + 1
y = e.Bounds.Top + 1
g.DrawString(s, CType(sender, ComboBox).Font, brText, x, y)
End Sub
0 Kommentare:
Kommentar veröffentlichen
<< Zurück