DataGridViewColumnCollection.Insert Method (Int32, DataGridViewColumn)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Inserts a column at the given index in the collection.

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

public virtual void Insert(
	int columnIndex,
	DataGridViewColumn dataGridViewColumn


Type: System.Int32

The zero-based index at which to insert the given column.

Type: System.Windows.Forms.DataGridViewColumn

The DataGridViewColumn to insert.

Exception Condition

dataGridViewColumn is null.


The associated DataGridView control is performing one of the following actions that temporarily prevents new columns from being added:

  • Selecting all cells in the control.

  • Clearing the selection.

  • Updating column DisplayIndex property values.


This method is being called from a handler for one of the following DataGridView events:


dataGridViewColumn already belongs to a DataGridView control.


The dataGridViewColumnSortMode property value is Automatic and the DataGridView.SelectionMode property value is FullColumnSelect or ColumnHeaderSelect. Use the control ISupportInitialize.BeginInit and ISupportInitialize.EndInit methods to temporarily set conflicting property values.


The dataGridViewColumnInheritedAutoSizeMode property value is ColumnHeader and the DataGridView.ColumnHeadersVisible property value is false.


dataGridViewColumn has an InheritedAutoSizeMode property value of Fill and a Frozen property value of true.


dataGridViewColumn has DisplayIndex and Frozen property values that would display it among a set of adjacent columns with the opposite Frozen property value.


The DataGridView control contains at least one row and dataGridViewColumn has a CellType property value of null.

The following code example illustrates the use of this method. For more information, see How to: Add an Unbound Column to a Data-Bound Windows Forms DataGridView Control.

private void CreateUnboundButtonColumn()
    // Initialize the button column.
    DataGridViewButtonColumn buttonColumn =
        new DataGridViewButtonColumn();
    buttonColumn.Name = "Details";
    buttonColumn.HeaderText = "Details";
    buttonColumn.Text = "View Details";

    // Use the Text property for the button text for all cells rather
    // than using each cell's value as the text for its own button.
    buttonColumn.UseColumnTextForButtonValue = true;

    // Add the button column to the control.
    dataGridView1.Columns.Insert(0, buttonColumn);

.NET Framework
Available since 2.0
Return to top