DataGridViewColumnCollection.Add Method (DataGridViewColumn)

 

Adds the given column to the collection.

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

public virtual int Add(
	DataGridViewColumn dataGridViewColumn
)

Parameters

dataGridViewColumn
Type: System.Windows.Forms.DataGridViewColumn

The DataGridViewColumn to add.

Return Value

Type: System.Int32

The index of the column.

Exception Condition
ArgumentNullException

dataGridViewColumn is null.

InvalidOperationException

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 DataGridView.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 DataGridView.ColumnHeadersVisible property value is false.

-or-

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

-or-

dataGridViewColumn has a FillWeight property value that would cause the combined FillWeight values of all columns in the control to exceed 65535.

-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.

The following code example illustrates the use of this method.

private DataGridView dataGridView1 = new DataGridView();

private void AddColorColumn()
{
    DataGridViewComboBoxColumn comboBoxColumn =
        new DataGridViewComboBoxColumn();
    comboBoxColumn.Items.AddRange(
        Color.Red, Color.Yellow, Color.Green, Color.Blue);
    comboBoxColumn.ValueType = typeof(Color);
    dataGridView1.Columns.Add(comboBoxColumn);
    dataGridView1.EditingControlShowing +=
        new DataGridViewEditingControlShowingEventHandler(
        dataGridView1_EditingControlShowing);
}

private void dataGridView1_EditingControlShowing(object sender,
    DataGridViewEditingControlShowingEventArgs e)
{
    ComboBox combo = e.Control as ComboBox;
    if (combo != null)
    {
        // Remove an existing event-handler, if present, to avoid 
        // adding multiple handlers when the editing control is reused.
        combo.SelectedIndexChanged -=
            new EventHandler(ComboBox_SelectedIndexChanged);

        // Add the event handler. 
        combo.SelectedIndexChanged +=
            new EventHandler(ComboBox_SelectedIndexChanged);
    }
}

private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    ((ComboBox)sender).BackColor = (Color)((ComboBox)sender).SelectedItem;
}

.NET Framework
Available since 2.0
Return to top
Show: