Using a BindingSource component to bind a list to a DataGridView control
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form
Private WithEvents changeItemBtn As New Button()
Private customersDataGridView As New DataGridView()
Private WithEvents customersBindingSource As New BindingSource()
Public Sub New()
Me.changeItemBtn.Text = "Change Item"
Me.changeItemBtn.Dock = DockStyle.Bottom
Me.Controls.Add(Me.changeItemBtn)
customersDataGridView.Dock = DockStyle.Top
Me.Controls.Add(customersDataGridView)
Me.Size = New Size(800, 200)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Me.Load
Dim customerList As List(Of DemoCustomer) = New List(Of DemoCustomer)
customerList.Add(New DemoCustomer())
customerList.Add(New DemoCustomer())
customerList.Add(New DemoCustomer())
Me.customersBindingSource.DataSource = customerList
Me.customersDataGridView.DataSource = Me.customersBindingSource
End Sub
Private Sub changeItemBtn_Click(ByVal sender As Object, ByVal e As EventArgs)Handles changeItemBtn.Click
Dim customerList As List(Of DemoCustomer) = CType(Me.customersBindingSource.DataSource, List(Of DemoCustomer))
customerList(0).CompanyName = "A"
Me.customersBindingSource.ResetItem(0)
End Sub
<STAThread()> _
Shared Sub Main()
Application.Run(New Form1())
End Sub
End Class
Public Class DemoCustomer
Private idValue As Guid = Guid.NewGuid()
Private customerName As String = String.Empty
Private companyNameValue As String = String.Empty
Sub New()
customerName = "no data"
companyNameValue = "no data"
End Sub
Public ReadOnly Property ID() As Guid
Get
Return Me.idValue
End Get
End Property
Public Property CompanyName() As String
Get
Return Me.companyNameValue
End Get
Set(ByVal value As String)
Me.companyNameValue = Value
End Set
End Property
End Class
Related examples in the same category