DataGridViewColumnCollection.Add 方法

定义

将给定的列添加到集合。

重载

Add(DataGridViewColumn)

将给定的列添加到集合。

Add(String, String)

将具有给定列名和列标题文本的 DataGridViewTextBoxColumn 添加到集合中。

Add(DataGridViewColumn)

将给定的列添加到集合。

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

参数

dataGridViewColumn
DataGridViewColumn

要添加的 DataGridViewColumn

返回

Int32

列的索引。

例外

dataGridViewColumnnull

关联的 DataGridView 控件正在执行以下操作之一,该操作暂时禁止添加新列:

示例

下面的代码示例演示了此方法的使用。

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

另请参阅

适用于

Add(String, String)

将具有给定列名和列标题文本的 DataGridViewTextBoxColumn 添加到集合中。

public:
 virtual int Add(System::String ^ columnName, System::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

参数

columnName
String

将用来指代列的名称。

headerText
String

该列的标题的文本。

返回

Int32

列的索引。

例外

关联的 DataGridView 控件正在执行以下操作之一,该操作暂时禁止添加新列:

注解

columnName``headerText参数分别与NameHeaderText属性相关。

另请参阅

适用于