Export (0) Print
Expand All
Expand Minimize

DataGridView.AutoGenerateColumns Property

Gets or sets a value indicating whether columns are created automatically when the DataSource or DataMember properties are set.

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

'Declaration
<BrowsableAttribute(False)> _
Public Property AutoGenerateColumns As Boolean

Property Value

Type: System.Boolean
true if the columns should be created automatically; otherwise, false. The default is true.

Columns are automatically generated when this property is set to true and the DataSource or DataMember properties are set or changed. Columns can also be automatically generated when the AutoGenerateColumns property is changed from false to true. If this property is true and the DataSource changes so there are columns that do not match the columns of the previous DataSource value, data in the unmatched columns is discarded. This property is ignored if the DataSource or DataMember properties are not set.

When AutoGenerateColumns is set to true, the DataGridView control generates one column for each public property of the objects in the data source. If the bound objects implement the ICustomTypeDescriptor interface, the control generates one column for each property returned by the GetProperties method. Each column header will contain the value of the property name the column represents.

If you set the DataSource property but set AutoGenerateColumns to false, you must add columns manually. You can bind each added column to the data source by setting the DataGridViewColumn.DataPropertyName property to the name of a property exposed by the bound objects.

NoteNote

Setting the DataSource in the Windows Forms Designer automatically sets the AutoGenerateColumns property to false and generates code to add and bind a column for each property in the data source. The code that is generated at design-time is equivalent to the manually added code shown in the following example. It is not the same as the auto-generation of columns at run-time that occurs when the AutoGenerateColumns property is set to true.

The following code example demonstrates how to add columns manually and bind them to a data source when you set AutoGenerateColumns to false. In this example, a DataGridView control is bound to a list of Task business objects. Then, columns are added and are bound to Task properties through the DataGridViewColumn.DataPropertyName property. This example is part of a larger example available in How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List.

Public Class Form1
    Inherits Form

    Private employees As New List(Of Employee)
    Private tasks As New List(Of Task)
    Private WithEvents reportButton As New Button
    Private WithEvents dataGridView1 As New DataGridView

    <STAThread()> _
    Public Sub Main()
        Application.Run(New Form1)
    End Sub 

    Sub New()
        dataGridView1.Dock = DockStyle.Fill
        dataGridView1.AutoSizeColumnsMode = _
            DataGridViewAutoSizeColumnsMode.AllCells
        reportButton.Text = "Generate Report"
        reportButton.Dock = DockStyle.Top

        Controls.Add(dataGridView1)
        Controls.Add(reportButton)
        Text = "DataGridViewComboBoxColumn Demo" 
    End Sub 

    ' Initializes the data source and populates the DataGridView control. 
    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As EventArgs) Handles Me.Load

        PopulateLists()
        dataGridView1.AutoGenerateColumns = False
        dataGridView1.DataSource = tasks
        AddColumns()

    End Sub 

    ' Populates the employees and tasks lists.  
    Private Sub PopulateLists()
        employees.Add(New Employee("Harry"))
        employees.Add(New Employee("Sally"))
        employees.Add(New Employee("Roy"))
        employees.Add(New Employee("Pris"))
        tasks.Add(New Task(1, employees(1)))
        tasks.Add(New Task(2))
        tasks.Add(New Task(3, employees(2)))
        tasks.Add(New Task(4))
    End Sub 

    ' Configures columns for the DataGridView control. 
    Private Sub AddColumns()

        Dim idColumn As New DataGridViewTextBoxColumn()
        idColumn.Name = "Task"
        idColumn.DataPropertyName = "Id"
        idColumn.ReadOnly = True 

        Dim assignedToColumn As New DataGridViewComboBoxColumn()

        ' Populate the combo box drop-down list with Employee objects.  
        For Each e As Employee In employees
            assignedToColumn.Items.Add(e)
        Next 

        ' Add "unassigned" to the drop-down list and display it for  
        ' empty AssignedTo values or when the user presses CTRL+0. 
        assignedToColumn.Items.Add("unassigned")
        assignedToColumn.DefaultCellStyle.NullValue = "unassigned"

        assignedToColumn.Name = "Assigned To"
        assignedToColumn.DataPropertyName = "AssignedTo"
        assignedToColumn.AutoComplete = True
        assignedToColumn.DisplayMember = "Name"
        assignedToColumn.ValueMember = "Self" 

        ' Add a button column.  
        Dim buttonColumn As New DataGridViewButtonColumn()
        buttonColumn.HeaderText = ""
        buttonColumn.Name = "Status Request"
        buttonColumn.Text = "Request Status"
        buttonColumn.UseColumnTextForButtonValue = True

        dataGridView1.Columns.Add(idColumn)
        dataGridView1.Columns.Add(assignedToColumn)
        dataGridView1.Columns.Add(buttonColumn)

    End Sub 

    ' Reports on task assignments.  
    Private Sub reportButton_Click(ByVal sender As Object, _
        ByVal e As EventArgs) Handles reportButton.Click

        Dim report As New StringBuilder()
        For Each t As Task In tasks
            Dim assignment As String 
            If t.AssignedTo Is Nothing Then
                assignment = "unassigned" 
            Else
                assignment = "assigned to " + t.AssignedTo.Name
            End If
            report.AppendFormat("Task {0} is {1}.", t.Id, assignment)
            report.Append(Environment.NewLine)
        Next
        MessageBox.Show(report.ToString(), "Task Assignments")

    End Sub 

    ' Calls the Employee.RequestStatus method. 
    Private Sub dataGridView1_CellClick(ByVal sender As Object, _
        ByVal e As DataGridViewCellEventArgs) _
        Handles dataGridView1.CellClick

        ' Ignore clicks that are not on button cells.  
        If e.RowIndex < 0 OrElse Not e.ColumnIndex = _
            dataGridView1.Columns("Status Request").Index Then Return 

        ' Retrieve the task ID. 
        Dim taskID As Int32 = CInt(dataGridView1(0, e.RowIndex).Value)

        ' Retrieve the Employee object from the "Assigned To" cell. 
        Dim assignedTo As Employee = TryCast(dataGridView1.Rows(e.RowIndex) _
            .Cells("Assigned To").Value, Employee)

        ' Request status through the Employee object if present.  
        If assignedTo IsNot Nothing Then
            assignedTo.RequestStatus(taskID)
        Else
            MessageBox.Show(String.Format( _
                "Task {0} is unassigned.", taskID), "Status Request")
        End If 

    End Sub 

End Class

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft