Skip to content

Instantly share code, notes, and snippets.

@tamago324
Created November 24, 2017 06:18
Show Gist options
  • Save tamago324/61e3dd4a9fbd01614ea2675a39b59555 to your computer and use it in GitHub Desktop.
Save tamago324/61e3dd4a9fbd01614ea2675a39b59555 to your computer and use it in GitHub Desktop.
DataGridViewにList(Of クラス)のデータを表示
' DataGridView1 というDataGridViewがあるとき
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' DataSourceの設定によって、自動で列が追加されないようにする
DataGridView1.AutoGenerateColumns = False
' 列の追加
Dim col1 As New DataGridViewColumn
col1.Name = "id"
col1.HeaderText = "ID"
col1.DataPropertyName = "Id"
col1.CellTemplate = New DataGridViewTextBoxCell()
DataGridView1.Columns.Add(col1)
Dim col2 As New DataGridViewColumn
col2.Name = "name"
col2.HeaderText = "NAME"
col2.DataPropertyName = "Name"
col2.CellTemplate = New DataGridViewTextBoxCell()
DataGridView1.Columns.Add(col2)
' 入れるテストデータ
Dim testList As New List(Of Test)
testList.Add(New Test("1", "Taro"))
testList.Add(New Test("2", "Jiro"))
' DataGridViewにバインド
DataGridView1.DataSource = testList
End Sub
End Class
Public Class Test
Public Property Id As String
Public Property Name As String
Public Sub New(ByVal id As String, ByVal name As String)
Me.Id = id
Me.Name = name
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment