DataGridViewColumnCollection.AddRange(DataGridViewColumn[]) Method

Definition

Adds a range of columns to the collection.

C#
public virtual void AddRange(params System.Windows.Forms.DataGridViewColumn[] dataGridViewColumns);

Parameters

dataGridViewColumns
DataGridViewColumn[]

An array of DataGridViewColumn objects to add.

Exceptions

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

At least one of the values in dataGridViewColumns is null.

-or-

At least one of the columns in dataGridViewColumns already belongs to a DataGridView control.

-or-

At least one of the columns in dataGridViewColumns has a CellType property value of null and the DataGridView control contains at least one row.

-or-

At least one of the columns in dataGridViewColumns has a SortMode property value of 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-

At least one of the columns in dataGridViewColumns has an InheritedAutoSizeMode property value of ColumnHeader and the ColumnHeadersVisible property value is false.

-or-

At least one of the columns in dataGridViewColumns has an InheritedAutoSizeMode property value of Fill and a Frozen property value of true.

-or-

The columns in dataGridViewColumns have FillWeight property values that would cause the combined FillWeight values of all columns in the control to exceed 65535.

-or-

At least two of the values in dataGridViewColumns are references to the same DataGridViewColumn.

-or-

At least one of the columns in dataGridViewColumns has DisplayIndex and Frozen property values that would display it among a set of adjacent columns with the opposite Frozen property value.

Examples

The following code example illustrates the use of this method. This example is part of a larger example available in How to: Set the Sizing Modes of the Windows Forms DataGridView Control.

C#
dataGridView1.Columns.AddRange(new DataGridViewTextBoxColumn[] { 
    idColumn, titleColumn, subTitleColumn, 
    summaryColumn, contentColumn });
dataGridView1.Rows.Add(new String[] { "1", 
    "A Short Title", "A Longer SubTitle", 
    "A short description of the main point.", 
    "The full contents of the topic, with detailed examples." });

Applies to

Produkt Versioner
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also