ListView und Datenbindung

31/08/2008 - 08:35 von Karsten Sosna | Report spam
Hallo NG,
ich habe eine Klasse, ganz simpel:
\\\
<Serilizable()> _
Public Class MySettings
Private _Settings As New List(Of String)

Public Property Settings As List(Of String)
Get
Return _Settings
End Get
Set(value As List(Of String)
_Settings = value
End Set
End Property
End Class
///
Wie kann ich diese an ein ListView binden und zwar so, dass ich die Texte
der ListViewItems in meiner List(Settings) habe?

Danke für jeden Tipp.
Gruß Scotty
 

Lesen sie die antworten

#1 Peter Fleischer
31/08/2008 - 18:58 | Warnen spam
"Karsten Sosna" schrieb im Newsbeitrag
news:

Wie kann ich diese an ein ListView binden und zwar so, dass ich die Texte
der ListViewItems in meiner List(Settings) habe?



Hi Karsten,
das ListView bietet keine Bindung an eine Datenquelle. Da musst du selbst
Hand anlegen und die Items laden. Das kannst du mit einer eigenen
ListView-Klasse machen, z.B. so (VB9):

Option Infer On
Option Strict On

Imports System.ComponentModel
Imports System.Drawing.Design

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load

Dim liste As New MySettings
liste.Settings.Add("Zeile 1")
liste.Settings.Add("Zeile 2")
liste.Settings.Add("Zeile 3")

Dim lv As New ListViewDB With {.Dock = DockStyle.Fill, _
.DataSource = liste.Settings}
Me.Controls.Add(lv)

End Sub

End Class

<Serializable()> _
Public Class MySettings
Private _Settings As New List(Of String)

Public Property Settings() As List(Of String)
Get
Return _Settings
End Get
Set(ByVal value As List(Of String))
_Settings = value
End Set
End Property
End Class

Public Class ListViewDB
Inherits ListView
Dim bs As BindingSource

Public Sub ListViewEx()
bs = New BindingSource()
Me.View = Windows.Forms.View.Details
Me.FullRowSelect = True
Me.GridLines = True
AddHandler bs.ListChanged, AddressOf OnListChanged
End Sub

<AttributeProvider(GetType(IListSource))> _
Public Property DataSource() As Object
Get
Return bs.DataSource
End Get
Set(ByVal value As Object)
If bs Is Nothing Then ListViewEx()
bs.DataSource = value
End Set
End Property

<Editor("System.Windows.Forms.Design.DataMemberListEditor, " & _
"System.Design, Version=2.0.0.0, Culture=neutral, " & _
"PublicKeyToken°3f5f7f11d50a3a", GetType(UITypeEditor))> _
Public Property DataMember() As String
Get
Return bs.DataMember
End Get
Set(ByVal value As String)
bs.DataMember = value
End Set
End Property

Private Sub OnListChanged(ByVal sender As Object, _
ByVal e As ListChangedEventArgs)
Me.SetColumnHeaders()
Me.SetValues()
End Sub

Private Sub SetColumnHeaders()
If (bs.List.Count > 0) Then
Me.Columns.Clear()
Dim dataItem As Object = bs.List(0)
For Each prop As PropertyDescriptor In
TypeDescriptor.GetProperties(dataItem)
If prop.PropertyType.GetInterface("IList") Is Nothing Then
If prop.ComponentType Is GetType(String) Then
Me.Columns.Add("Value")
Else
Me.Columns.Add(prop.DisplayName)
End If
End If
Next
End If
End Sub

Private Sub SetValues()
Me.Items.Clear()
For Each dataItem As Object In bs.List
If dataItem.GetType Is GetType(String) Then
Me.Items.Add(dataItem.ToString)
ElseIf TypeDescriptor.GetProperties(dataItem).Count = 0 _
AndAlso dataItem.GetType.IsValueType Then
If Me.Columns.Count = 0 Then
Me.Columns.Add("Value")
Me.Items.Add(dataItem.ToString())
Else
Dim props = TypeDescriptor.GetProperties(dataItem)
Dim valueList = New List(Of String)
For Each propDesc As PropertyDescriptor In props
Dim value As String = If(propDesc.GetValue(dataItem) Is Nothing,
_
String.Empty,
propDesc.GetValue(dataItem).ToString())
valueList.Add(value)
Me.Items.Add((New ListViewItem(valueList.ToArray())))
Next
End If
End If
Next

End Sub

End Class

Viele Gruesse

Peter

Ähnliche fragen