簡単なデータクラスは次のとおりです。
Public Class MyData
Private _ID As Integer
Private _ItemValue As String
Public Sub New(ByVal id As Integer, ByVal itemValue As String)
_ID = id
_ItemValue = itemValue
End Sub
ReadOnly Property ID() As Integer
Get
Return _ID
End Get
End Property
Public Property ItemValue() As String
Get
Return _ItemValue
End Get
Set(ByVal value As String)
_ItemValue = value
End Set
End Property
End Class
フォームを作成してDataGridViewコントロールを配置し、次のコードを追加します。
Private myList As New List(Of String)
Private myItems As New List(Of MyData)
Protected Overrides Sub OnLoad(ByVal e As EventArgs)
MyBase.OnLoad(e)
myList.Add("First Item")
myList.Add("Last Item")
myItems.Add(New MyData(1, "Last Item"))
myItems.Add(New MyData(2, "First Item"))
DataGridView1.AutoGenerateColumns = False
DataGridView1.Columns.Add(New DataGridViewTextBoxColumn() With _
{.HeaderText = "ID", _
.DataPropertyName = "ID"})
DataGridView1.Columns.Add(New DataGridViewComboBoxColumn() With _
{.HeaderText = "ItemValue", _
.DataSource = myList, _
.DataPropertyName = "ItemValue"})
DataGridView1.DataSource = myItems
End Sub
グリッドにコンボボックスが必要なためAutoGenerateColumns
、falseに設定して自分で作成し、プロパティを介して各列をクラスのプロパティにマッピングする必要がありDataPropertyName
ます。DataSource
コンボボックスの場合、ドロップダウンアイテムのリストに独自のコンボボックスを設定できます。