Export (0) Print
Expand All

DataGridViewColumn.Name Property

Gets or sets the name of the column.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
<BrowsableAttribute(False)> _
Public Property Name As String

Property Value

Type: System.String
A String that contains the name of the column. The default is an empty string ("").

This property represents a formal name associated with the column that can be used to identify the column in a collection. For example, the Remove and Contains methods of the DataGridViewColumnCollection class use the Name property. The name is case-insensitive. The DataGridView will treat column1 and COLUMN1 as the same column.

The following code example shows how to set the column name.

    Private Sub SetupGrid()
        knights = New List(Of Knight)
        knights.Add(New Knight(Title.King, "Uther", True))
        knights.Add(New Knight(Title.King, "Arthur", True))
        knights.Add(New Knight(Title.Sir, "Mordred", False))
        knights.Add(New Knight(Title.Sir, "Gawain", True))
        knights.Add(New Knight(Title.Sir, "Galahad", True))

        ' Initialize the DataGridView.
        dataGridView1.AutoGenerateColumns = False
        dataGridView1.AutoSize = True
        dataGridView1.DataSource = knights

        dataGridView1.Columns.Add(CreateComboBoxWithEnums())

        ' Initialize and add a text box column. 
        Dim column As DataGridViewColumn = _
            New DataGridViewTextBoxColumn()
        column.DataPropertyName = "Name"
        column.Name = "Knight"
        dataGridView1.Columns.Add(column)

        ' Initialize and add a check box column.
        column = New DataGridViewCheckBoxColumn()
        column.DataPropertyName = "GoodGuy"
        column.Name = "Good"
        dataGridView1.Columns.Add(column)

        ' Initialize the form.
        Controls.Add(dataGridView1)
        Me.AutoSize = True 
        Me.Text = "DataGridView object binding demo" 
    End Sub 

    Private Function CreateComboBoxWithEnums() As DataGridViewComboBoxColumn
        Dim combo As New DataGridViewComboBoxColumn()
        combo.DataSource = [Enum].GetValues(GetType(Title))
        combo.DataPropertyName = "Title"
        combo.Name = "Title" 
        Return combo
    End Function

#Region "business object" 
    Private Class Knight
        Private hisName As String 
        Private good As Boolean 
        Private hisTitle As Title

        Public Sub New(ByVal title As Title, ByVal name As String, _
            ByVal good As Boolean)

            hisTitle = title
            hisName = name
            Me.good = good
        End Sub 

        Public Property Name() As String 
            Get 
                Return hisName
            End Get 

            Set(ByVal Value As String)
                hisName = Value
            End Set 
        End Property 

        Public Property GoodGuy() As Boolean 
            Get 
                Return good
            End Get 
            Set(ByVal Value As Boolean)
                good = Value
            End Set 
        End Property 

        Public Property Title() As Title
            Get 
                Return hisTitle
            End Get 
            Set(ByVal Value As Title)
                hisTitle = Value
            End Set 
        End Property 
    End Class
#End Region

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft