Επεξεργασία

Κοινή χρήση μέσω


DataGridViewColumnCollection.Add Method

Definition

Adds the given column to the collection.

Overloads

Add(DataGridViewColumn)

Adds the given column to the collection.

Add(String, String)

Adds a DataGridViewTextBoxColumn with the given column name and column header text to the collection.

Add(DataGridViewColumn)

Source:
DataGridViewColumnCollection.cs
Source:
DataGridViewColumnCollection.cs
Source:
DataGridViewColumnCollection.cs

Adds the given column to the collection.

public:
 virtual int Add(System::Windows::Forms::DataGridViewColumn ^ dataGridViewColumn);
public virtual int Add (System.Windows.Forms.DataGridViewColumn dataGridViewColumn);
abstract member Add : System.Windows.Forms.DataGridViewColumn -> int
override this.Add : System.Windows.Forms.DataGridViewColumn -> int
Public Overridable Function Add (dataGridViewColumn As DataGridViewColumn) As Integer

Parameters

dataGridViewColumn
DataGridViewColumn

The DataGridViewColumn to add.

Returns

The index of the column.

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

Examples

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;
}
Private WithEvents dataGridView1 As New DataGridView()

Private Sub AddColorColumn()

    Dim comboBoxColumn As New DataGridViewComboBoxColumn()
    comboBoxColumn.Items.AddRange( _
        Color.Red, Color.Yellow, Color.Green, Color.Blue)
    comboBoxColumn.ValueType = GetType(Color)
    dataGridView1.Columns.Add(comboBoxColumn)

End Sub

Private Sub dataGridView1_EditingControlShowing(ByVal sender As Object, _
    ByVal e As DataGridViewEditingControlShowingEventArgs) _
    Handles dataGridView1.EditingControlShowing

    Dim combo As ComboBox = CType(e.Control, ComboBox)
    If (combo IsNot Nothing) Then

        ' Remove an existing event-handler, if present, to avoid 
        ' adding multiple handlers when the editing control is reused.
        RemoveHandler combo.SelectedIndexChanged, _
            New EventHandler(AddressOf ComboBox_SelectedIndexChanged)

        ' Add the event handler. 
        AddHandler combo.SelectedIndexChanged, _
            New EventHandler(AddressOf ComboBox_SelectedIndexChanged)

    End If

End Sub

Private Sub ComboBox_SelectedIndexChanged( _
    ByVal sender As Object, ByVal e As EventArgs)

    Dim comboBox1 As ComboBox = CType(sender, ComboBox)
    comboBox1.BackColor = _
        CType(CType(sender, ComboBox).SelectedItem, Color)

End Sub

See also

Applies to

Add(String, String)

Source:
DataGridViewColumnCollection.cs
Source:
DataGridViewColumnCollection.cs
Source:
DataGridViewColumnCollection.cs

Adds a DataGridViewTextBoxColumn with the given column name and column header text to the collection.

public:
 virtual int Add(System::String ^ columnName, System::String ^ headerText);
public virtual int Add (string columnName, string headerText);
public virtual int Add (string? columnName, string? headerText);
abstract member Add : string * string -> int
override this.Add : string * string -> int
Public Overridable Function Add (columnName As String, headerText As String) As Integer

Parameters

columnName
String

The name by which the column will be referred.

headerText
String

The text for the column's header.

Returns

The index of the column.

Exceptions

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-

The SelectionMode property value is FullColumnSelect or ColumnHeaderSelect, which conflicts with the default column SortMode property value of Automatic.

-or-

The default column FillWeight property value of 100 would cause the combined FillWeight values of all columns in the control to exceed 65535.

Remarks

The columnName and headerText parameters are related to the Name and HeaderText properties, respectively.

See also

Applies to