แก้ไข

แชร์ผ่าน


DataGridViewRowCollection.InsertRange(Int32, DataGridViewRow[]) Method

Definition

Inserts the DataGridViewRow objects into the collection at the specified position.

public:
 virtual void InsertRange(int rowIndex, ... cli::array <System::Windows::Forms::DataGridViewRow ^> ^ dataGridViewRows);
public virtual void InsertRange (int rowIndex, params System.Windows.Forms.DataGridViewRow[] dataGridViewRows);
abstract member InsertRange : int * System.Windows.Forms.DataGridViewRow[] -> unit
override this.InsertRange : int * System.Windows.Forms.DataGridViewRow[] -> unit
Public Overridable Sub InsertRange (rowIndex As Integer, ParamArray dataGridViewRows As DataGridViewRow())

Parameters

rowIndex
Int32

The position at which to insert the rows.

dataGridViewRows
DataGridViewRow[]

An array of DataGridViewRow objects to add to the DataGridViewRowCollection.

Exceptions

dataGridViewRows is null.

rowIndex is less than zero or greater than the number of rows in the collection.

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

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-

rowIndex is equal to the number of rows in the collection and AllowUserToAddRows is true.

-or-

The DataSource property of the DataGridView is not null.

-or-

At least one entry in the dataGridViewRows array is null.

-or-

The DataGridView has no columns.

-or-

At least one row in the dataGridViewRows array has a DataGridView property value that is not null.

-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 insert frozen rows after unfrozen rows or unfrozen rows before frozen rows.

Examples

The following code example demonstrates how to use the InsertRange method to insert rows before selected rows to work around the bug indicated in the Remarks section.

// 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;
    }
}
' Workaround for bug that prevents DataGridViewRowCollection.InsertRange
' from working when any rows before the insertion index are selected.
Private Sub InsertRows(ByVal index As Integer, _
    ByVal ParamArray rows As DataGridViewRow())

    Dim selectedIndexes As New System.Collections.Generic.List(Of Integer)

    For Each row As DataGridViewRow In dataGridView1.SelectedRows
        If row.Index >= index Then
            selectedIndexes.Add(row.Index)
            row.Selected = False
        End If
    Next row

    dataGridView1.Rows.InsertRange(index, rows)

    For Each selectedIndex As Integer In selectedIndexes
        dataGridView1.Rows(selectedIndex).Selected = True
    Next selectedIndex

End Sub

Remarks

The InsertRange 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 DataGridView.Sort method in a DataGridView.RowsAdded event handler. You might also want to call the DataGridView.Sort method in a CellValueChanged event handler to sort the rows when the user modifies a cell.

Important

Due to a bug, the InsertRange method will make your application stop responding if any rows with an index greater than rowIndex are selected. To work around this bug, you must cancel the selection of these rows before calling this method, and reselect the rows afterward. For more information, see the code example in this topic.

Applies to

See also