Editar

Partilhar via


DataGridViewColumnCollection.Insert(Int32, DataGridViewColumn) Method

Definition

Inserts a column at the given index in the collection.

public:
 virtual void Insert(int columnIndex, System::Windows::Forms::DataGridViewColumn ^ dataGridViewColumn);
public virtual void Insert (int columnIndex, System.Windows.Forms.DataGridViewColumn dataGridViewColumn);
abstract member Insert : int * System.Windows.Forms.DataGridViewColumn -> unit
override this.Insert : int * System.Windows.Forms.DataGridViewColumn -> unit
Public Overridable Sub Insert (columnIndex As Integer, dataGridViewColumn As DataGridViewColumn)

Parameters

columnIndex
Int32

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

dataGridViewColumn
DataGridViewColumn

The DataGridViewColumn to insert.

Exceptions

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.

-or-

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

-or-

dataGridViewColumn already belongs to a DataGridView control.

-or-

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

-or-

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

-or-

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

-or-

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

-or-

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

Examples

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);
}
Private Sub CreateUnboundButtonColumn()

    ' Initialize the button column.
    Dim buttonColumn As New DataGridViewButtonColumn

    With buttonColumn
        .HeaderText = "Details"
        .Name = "Details"
        .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.
        .UseColumnTextForButtonValue = True
    End With

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

End Sub

Applies to

See also