Export (0) Print
Expand All

DataGridViewColumnCollection.AddRange Method

Adds a range of columns to the collection.

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

public virtual void AddRange(
	params DataGridViewColumn[] dataGridViewColumns
)

Parameters

dataGridViewColumns
Type: System.Windows.Forms.DataGridViewColumn[]

An array of DataGridViewColumn objects to add.

ExceptionCondition
ArgumentNullException

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

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

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.

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." });

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft