VB.net DataGridViewのカラム(列)をコードで生成する方法

記事内に商品プロモーションを含む場合があります。
2018.3.11 22 views

データグリッドビューの列をコードで生成する方法

Private Sub createDgvColLv1(ByRef dgv As DataGridView)

    Dim txtCode As New DataGridViewTextBoxColumn()
    With txtCode
        .DataPropertyName = "111コード"
        .Name = .DataPropertyName
        .HeaderText = "コード"
        .Width = 60
        .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        .ReadOnly = False
    End With
    dgv.Columns.Add(txtCode)

    Dim txtName As New DataGridViewTextBoxColumn()
    With txtName
        .DataPropertyName = "111名称"
        .Name = .DataPropertyName
        .HeaderText = "111名称"
        .Width = 150
        .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
        .ReadOnly = False
    End With
    dgv.Columns.Add(txtName)

    Dim intDGVWidht As Integer = 3
    For Each col As DataGridViewColumn In dgv.Columns
        intDGVWidht = intDGVWidht + col.Width
    Next

    With dgv
        .RowsDefaultCellStyle.Font = New Font("MS UI Gothic", 10)
        .ReadOnly = True
        .AllowUserToDeleteRows = False
        .AllowUserToAddRows = False
        .MultiSelect = False
        .SelectionMode = DataGridViewSelectionMode.FullRowSelect
        .Width = intDGVWidht + SystemInformation.VerticalScrollBarWidth
        .RowHeadersVisible = False
        .AllowUserToResizeColumns = False
        .AllowUserToResizeRows = False
        .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        .CurrentCell = Nothing
    End With
End Sub