How to: Implement the IListSource Interface

How to: Implement the IListSource Interface

 

Implement the IListSource interface to create a bindable class that does not implement IList but instead provides a list from another location.

Example

The following code example demonstrates how to implement the IListSource interface. A component named EmployeeListSource exposes an IList for data binding by implementing the GetList method.

Imports System.ComponentModel

Public Class EmployeeListSource
    Inherits Component
    Implements IListSource

    <System.Diagnostics.DebuggerNonUserCode()> _
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
        MyClass.New()

        'Required for Windows.Forms Class Composition Designer support
        Container.Add(Me)

    End Sub

    <System.Diagnostics.DebuggerNonUserCode()> _
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

    End Sub

    'Component overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Component Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Component Designer
    'It can be modified using the Component Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()
    End Sub

#Region "IListSource Members"

    Public ReadOnly Property ContainsListCollection() As Boolean Implements System.ComponentModel.IListSource.ContainsListCollection
        Get
            Return False
        End Get
    End Property

    Public Function GetList() As System.Collections.IList Implements System.ComponentModel.IListSource.GetList

        Dim ble As New BindingList(Of Employee)

        If Not Me.DesignMode Then
            ble.Add(New Employee("Aaberg, Jesper", 26000000))
            ble.Add(New Employee("Cajhen, Janko", 19600000))
            ble.Add(New Employee("Furse, Kari", 19000000))
            ble.Add(New Employee("Langhorn, Carl", 16000000))
            ble.Add(New Employee("Todorov, Teodor", 15700000))
            ble.Add(New Employee("Vereb�lyi, �gnes", 15700000))
        End If

        Return ble

    End Function

#End Region

End Class
Imports System.ComponentModel

Public Class Employee
    Inherits BusinessObjectBase

    Private _id As String
    Private _name As String
    Private _parkingId As Decimal

    Public Sub New(ByVal name As String, ByVal parkId As Decimal)
        MyBase.New()
        Me._id = System.Guid.NewGuid().ToString()
        ' Set values
        Me.Name = name
        Me.ParkingID = parkId
    End Sub

    Public ReadOnly Property ID() As String
        Get
            Return _id
        End Get
    End Property

    Const NAME_Const As String = "Name"

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            If _name <> value Then
                _name = value
                OnPropertyChanged(NAME_Const)
            End If
        End Set
    End Property

    Const PARKINGID_Const As String = "ParkingID"

    Public Property ParkingID() As Decimal
        Get
            Return _parkingId
        End Get
        Set(ByVal value As Decimal)
            If _parkingId <> value Then
                _parkingId = value
                OnPropertyChanged(PARKINGID_Const)
            End If
        End Set
    End Property

End Class
Imports System.ComponentModel

Public Class BusinessObjectBase
    Implements INotifyPropertyChanged

#Region "INotifyPropertyChanged Members"

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Protected Overridable Sub OnPropertyChanged(ByVal propertyName As String)
        OnPropertyChanged(New PropertyChangedEventArgs(propertyName))
    End Sub

    Private Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
        RaiseEvent PropertyChanged(Me, e)
    End Sub

#End Region

End Class
Imports System.ComponentModel
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    Friend WithEvents flowLayoutPanel1 As FlowLayoutPanel
    Friend WithEvents label2 As Label
    Friend WithEvents dataGridView1 As DataGridView
    Friend WithEvents nameDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
    Friend WithEvents salaryDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
    Friend WithEvents iDDataGridViewTextBoxColumn As DataGridViewTextBoxColumn
    Friend WithEvents employeeListSource1 As EmployeeListSource

    Public Sub New()
        MyBase.New()

        Me.InitializeComponent()
    End Sub

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        components = New System.ComponentModel.Container()

        Dim dataGridViewCellStyle1 = New System.Windows.Forms.DataGridViewCellStyle()
        Dim dataGridViewCellStyle2 = New System.Windows.Forms.DataGridViewCellStyle()
        Me.flowLayoutPanel1 = New System.Windows.Forms.FlowLayoutPanel()
        Me.label2 = New System.Windows.Forms.Label()
        Me.dataGridView1 = New System.Windows.Forms.DataGridView()
        Me.nameDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.salaryDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.iDDataGridViewTextBoxColumn = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.employeeListSource1 = New EmployeeListSource(Me.components)
        Me.flowLayoutPanel1.SuspendLayout()
        CType(Me.dataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        ' 
        ' flowLayoutPanel1
        ' 
        Me.flowLayoutPanel1.AutoSize = True
        Me.flowLayoutPanel1.Controls.Add(Me.label2)
        Me.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Top
        Me.flowLayoutPanel1.Location = New System.Drawing.Point(0, 0)
        Me.flowLayoutPanel1.Name = "flowLayoutPanel1"
        Me.flowLayoutPanel1.Size = New System.Drawing.Size(416, 51)
        Me.flowLayoutPanel1.TabIndex = 11
        ' 
        ' label2
        ' 
        Me.label2.AutoSize = True
        Me.label2.Location = New System.Drawing.Point(3, 6)
        Me.label2.Margin = New System.Windows.Forms.Padding(3, 6, 3, 6)
        Me.label2.Name = "label2"
        Me.label2.Size = New System.Drawing.Size(408, 39)
        Me.label2.TabIndex = 0
        Me.label2.Text = "This sample demonstrates how to implement the IListSource interface.  In this sam" + _
            "ple, a DataGridView is bound at design time to a Component (employeeListSource1)" + _
            " that implements IListSource."
        ' 
        ' dataGridView1
        ' 
        Me.dataGridView1.AllowUserToAddRows = False
        Me.dataGridView1.AllowUserToDeleteRows = False
        dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(255, 255, 192)
        Me.dataGridView1.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1
        Me.dataGridView1.AutoGenerateColumns = False
        Me.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        Me.dataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() { _
        Me.nameDataGridViewTextBoxColumn, Me.salaryDataGridViewTextBoxColumn, Me.iDDataGridViewTextBoxColumn})
        Me.dataGridView1.DataSource = Me.employeeListSource1
        Me.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.dataGridView1.Location = New System.Drawing.Point(0, 51)
        Me.dataGridView1.Name = "dataGridView1"
        Me.dataGridView1.RowHeadersVisible = False
        Me.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
        Me.dataGridView1.Size = New System.Drawing.Size(416, 215)
        Me.dataGridView1.TabIndex = 12
        ' 
        ' nameDataGridViewTextBoxColumn
        ' 
        Me.nameDataGridViewTextBoxColumn.DataPropertyName = "Name"
        Me.nameDataGridViewTextBoxColumn.FillWeight = 131.7987F
        Me.nameDataGridViewTextBoxColumn.HeaderText = "Name"
        Me.nameDataGridViewTextBoxColumn.Name = "nameDataGridViewTextBoxColumn"
        ' 
        ' salaryDataGridViewTextBoxColumn
        ' 
        Me.salaryDataGridViewTextBoxColumn.DataPropertyName = "ParkingID"
        Me.salaryDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle2
        Me.salaryDataGridViewTextBoxColumn.FillWeight = 121.8274F
        Me.salaryDataGridViewTextBoxColumn.HeaderText = "Parking ID"
        Me.salaryDataGridViewTextBoxColumn.Name = "salaryDataGridViewTextBoxColumn"
        ' 
        ' iDDataGridViewTextBoxColumn
        ' 
        Me.iDDataGridViewTextBoxColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill
        Me.iDDataGridViewTextBoxColumn.DataPropertyName = "ID"
        Me.iDDataGridViewTextBoxColumn.FillWeight = 46.37391F
        Me.iDDataGridViewTextBoxColumn.HeaderText = "ID"
        Me.iDDataGridViewTextBoxColumn.Name = "iDDataGridViewTextBoxColumn"
        Me.iDDataGridViewTextBoxColumn.ReadOnly = True
        ' 
        ' Form1
        ' 
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0F, 13.0F)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(416, 266)
        Me.Controls.Add(Me.dataGridView1)
        Me.Controls.Add(Me.flowLayoutPanel1)
        Me.Name = "Form1"
        Me.Text = "IListSource Sample"
        Me.flowLayoutPanel1.ResumeLayout(False)
        Me.flowLayoutPanel1.PerformLayout()
        CType(Me.dataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Shared Sub Main()
        Application.Run(New Form1())
    End Sub
End Class

Compiling the Code

This example requires:

  • References to the System.Drawing and System.Windows.Forms assemblies.

Show:
© 2016 Microsoft