Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataGridViewRowCollection.AddRange Method

Note: This method is new in the .NET Framework version 2.0.

This method supports the .NET Framework infrastructure and is not intended to be used directly from your code.

Adds the specified DataGridViewRow objects to the collection.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public virtual void AddRange (
	params DataGridViewRow[] dataGridViewRows
)
public void AddRange (
	DataGridViewRow[] dataGridViewRows
)
public function AddRange (
	... dataGridViewRows : DataGridViewRow[]
)

Parameters

dataGridViewRows

An array of DataGridViewRow objects to be added to the DataGridViewRowCollection.

Exception typeCondition

ArgumentNullException

dataGridViewRows is a null reference (Nothing in Visual Basic).

ArgumentException

dataGridViewRows contains only one row, and the row it contains has more cells than there are columns in the control.

InvalidOperationException

The associated DataGridView control is performing one of the following actions that temporarily prevents new rows from being added:

  • Selecting all cells in the control.

  • Clearing the selection.

-or-

This method is being called from a handler for one of the following DataGridView events:

-or-

The DataSource property of the DataGridView is not a null reference (Nothing in Visual Basic).

-or-

At least one entry in the dataGridViewRows array is a null reference (Nothing in Visual Basic).

-or-

The DataGridView has no columns.

-or-

At least one row in the dataGridViewRows array has a DataGridView property value that is not a null reference (Nothing in Visual Basic).

-or-

At least one row in the dataGridViewRows array has a Selected property value of true.

-or-

Two or more rows in the dataGridViewRows array are identical.

-or-

At least one row in the dataGridViewRows array contains one or more cells of a type that is incompatible with the type of the corresponding column in the control.

-or-

At least one row in the dataGridViewRows array contains more cells than there are columns in the control.

-or-

This operation would add frozen rows after unfrozen rows.

The AddRange method adds shared rows to the DataGridViewRowCollection, if possible. Otherwise, the new rows are unshared. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.

Rows in the control are not automatically sorted when new rows are added. To sort new rows into their correct position, call the System.Windows.Forms.DataGridView.Sort method in a DataGridView.RowsAdded event handler. You might also want to call the System.Windows.Forms.DataGridView.Sort method in a CellValueChanged event handler to sort the rows when the user modifies a cell.

NoteImportant:

Due to a bug, the AddRange method will make your application stop responding if the AllowUserToAddRows property is true and the row for new records is selected. To work around this bug, you must cancel the selection of the row before calling this method, and then reselect the row. For more information, see the code example in this topic.

The following code example demonstrates how to use the AddRange method when the row for new records is selected to work around the bug indicated in the Remarks section.

// Workaround for bug that prevents DataGridViewRowCollection.AddRange
// from working when the row for new records is selected. 
private void AddRows(params DataGridViewRow[] rows)
{
    InsertRows(dataGridView1.RowCount - 1, rows);
}

// Workaround for bug that prevents DataGridViewRowCollection.InsertRange
// from working when any rows before the insertion index are selected.
private void InsertRows(int index, params DataGridViewRow[] rows)
{
    System.Collections.Generic.List<int> selectedIndexes =
        new System.Collections.Generic.List<int>();
    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
    {
        if (row.Index >= index)
        {
            selectedIndexes.Add(row.Index);
            row.Selected = false;
        }
    }
    dataGridView1.Rows.InsertRange(index, rows);
    foreach (int selectedIndex in selectedIndexes)
    {
        dataGridView1.Rows[selectedIndex].Selected = true;
    }
}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft