DataGridViewRowCollection.Insert Method

Definition

Inserts one or more rows into the collection.

Overloads

Insert(Int32, DataGridViewRow)

Inserts the specified DataGridViewRow into the collection.

Insert(Int32, Int32)

Inserts the specified number of rows into the collection at the specified location.

Insert(Int32, Object[])

Inserts a row into the collection at the specified position, and populates the cells with the specified objects.

Insert(Int32, DataGridViewRow)

Inserts the specified DataGridViewRow into the collection.

public:
 virtual void Insert(int rowIndex, System::Windows::Forms::DataGridViewRow ^ dataGridViewRow);
public virtual void Insert (int rowIndex, System.Windows.Forms.DataGridViewRow dataGridViewRow);
abstract member Insert : int * System.Windows.Forms.DataGridViewRow -> unit
override this.Insert : int * System.Windows.Forms.DataGridViewRow -> unit
Public Overridable Sub Insert (rowIndex As Integer, dataGridViewRow As DataGridViewRow)

Parameters

rowIndex
Int32

The position at which to insert the row.

dataGridViewRow
DataGridViewRow

The DataGridViewRow to insert into the DataGridViewRowCollection.

Exceptions

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

dataGridViewRow is null.

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

-or-

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

-or-

The DataGridView has no columns.

-or-

The DataGridView property of dataGridViewRow is not null.

-or-

dataGridViewRow has a Selected property value of true.

-or-

This operation would insert a frozen row after unfrozen rows or an unfrozen row before frozen rows.

dataGridViewRow has more cells than there are columns in the control.

Remarks

The Insert(Int32, DataGridViewRow) method adds a shared row to the DataGridViewRowCollection, if possible. Otherwise, the new row is 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.

See also

Applies to

Insert(Int32, Int32)

Inserts the specified number of rows into the collection at the specified location.

public:
 virtual void Insert(int rowIndex, int count);
public virtual void Insert (int rowIndex, int count);
abstract member Insert : int * int -> unit
override this.Insert : int * int -> unit
Public Overridable Sub Insert (rowIndex As Integer, count As Integer)

Parameters

rowIndex
Int32

The position at which to insert the rows.

count
Int32

The number of rows to insert into the DataGridViewRowCollection.

Exceptions

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

-or-

count is less than 1.

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

-or-

The DataGridView has no columns.

-or-

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

-or-

The row returned by the RowTemplate property has more cells than there are columns in the control.

-or-

This operation would insert a frozen row after unfrozen rows or an unfrozen row before frozen rows.

Remarks

The Insert(Int32, Int32) method adds rows that are based on the RowTemplate of the DataGridView. The new row is shared, if possible. Be sure that the row specified in the RowTemplate property can be shared for best scalability. 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.

See also

Applies to

Insert(Int32, Object[])

Inserts a row into the collection at the specified position, and populates the cells with the specified objects.

public:
 virtual void Insert(int rowIndex, ... cli::array <System::Object ^> ^ values);
public virtual void Insert (int rowIndex, params object[] values);
abstract member Insert : int * obj[] -> unit
override this.Insert : int * obj[] -> unit
Public Overridable Sub Insert (rowIndex As Integer, ParamArray values As Object())

Parameters

rowIndex
Int32

The position at which to insert the row.

values
Object[]

A variable number of objects that populate the cells of the new row.

Exceptions

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

values is null.

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 VirtualMode property of the DataGridView is set to true.

-or-

The DataSource property of the DataGridView is not null.

-or-

The DataGridView has no columns.

-or-

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

-or-

The DataGridView property of the row returned by the control's RowTemplate property is not null.

-or-

This operation would insert a frozen row after unfrozen rows or an unfrozen row before frozen rows.

The row returned by the control's RowTemplate property has more cells than there are columns in the control.

Remarks

The Insert(Int32, Object[]) method adds an unshared row to the DataGridViewRowCollection.

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.

See also

Applies to