如何为DataGridviewColumn类型声明变量?

时间:2019-01-31 16:18:40

标签: winforms datagridview variable-declaration datagridviewcolumn

这必须很简单,但是即使阅读Microsoft's specification,我也无法弄清楚。

如何将DataGridViewColumn类型存储在已声明的变量中?它必须提供类型的集合,类似于为DataGridViewContentAlignment声明变量:

Dim GridAlignment as DataGridViewContentAlignment          ' declare

GridAlignment = DataGridViewContentAlignment.MiddleCenter  ' set

所以应该是这样的:

Dim GridColType1 as DataGridViewColumn      ' declare variable (how?)
Dim GridColType2 as DataGridViewColumn      ' declare variable (how?)

' To use variable like this:
GridColType1 = DataGridViewColumn.DataGridViewTextBoxColumn    ' set it as TextBox column
GridColType2 = DataGridViewColumn.DataGridViewComboBoxColumn   ' set it as ComboBox column

在此阶段,不存在DataGridView,它是用于动态生成存储的定义,我需要正确的声明才能在PropertyGrid中使用它。

编辑: 换个说法,这是VS在添加/编辑DataGridView列时在GUI中的功能: enter image description here

因此,我要查找的是 ColumnType 声明,并在列表框中显示该集合。

1 个答案:

答案 0 :(得分:0)

好的,看来最好和最简单的方法是定义自定义枚举。

Public Enum DataGridViewColumnType
    NotSetError = 0
    TextBoxColumn = 1
    ImageColumn = 2
    CheckBoxColumn = 3
    ComboBoxColumn = 4
    ButtonCOlumn = 5
End Enum

然后我可以设置该类型的变量:

Public GridColType As DataGridViewColumnType

然后,它可以在属性被使用:

<CategoryAttribute("Grid"), DefaultValueAttribute(""), DescriptionAttribute("Select column type"), DisplayName("Grid Column Type")>
Public Property PropGriColType() As DataGridViewColumnType
    Get
        Return GridColType
    End Get
    Set(ByVal Value As DataGridViewColumnType)
        GridColType = Value
    End Set
End Property

结果然后根据需要:

enter image description here