Edit

Share via


DataGridViewRowCollection.Add Method

Definition

Adds one or more rows to the collection.

Overloads

Add()

Adds a new row to the collection.

Add(Int32)

Adds the specified number of new rows to the collection.

Add(Object[])

Adds a new row to the collection, and populates the cells with the specified objects.

Add(DataGridViewRow)

Adds the specified DataGridViewRow to the collection.

Add()

Adds a new row to the collection.

public:
 virtual int Add();
public virtual int Add ();
abstract member Add : unit -> int
override this.Add : unit -> int
Public Overridable Function Add () As Integer

Returns

The index of the new row.

Exceptions

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-

This operation would add a frozen row after unfrozen rows.

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

Remarks

The Add() overload 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

Add(Int32)

Adds the specified number of new rows to the collection.

public:
 virtual int Add(int count);
public virtual int Add (int count);
abstract member Add : int -> int
override this.Add : int -> int
Public Overridable Function Add (count As Integer) As Integer

Parameters

count
Int32

The number of rows to add to the DataGridViewRowCollection.

Returns

The index of the last row that was added.

Exceptions

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-

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

-or-

This operation would add frozen rows after unfrozen rows.

Remarks

The Add(Int32) method adds shared rows to the DataGridViewRowCollection. The new rows are based on the RowTemplate of the DataGridView. 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

Add(Object[])

Adds a new row to the collection, and populates the cells with the specified objects.

public:
 virtual int Add(... cli::array <System::Object ^> ^ values);
public virtual int Add (params object[] values);
abstract member Add : obj[] -> int
override this.Add : obj[] -> int
Public Overridable Function Add (ParamArray values As Object()) As Integer

Parameters

values
Object[]

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

Returns

The index of the new row.

Exceptions

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-

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

-or-

This operation would add a frozen row after unfrozen rows.

Remarks

The Add(Object[]) method adds an unshared row to the DataGridViewRowCollection. 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

Add(DataGridViewRow)

Adds the specified DataGridViewRow to the collection.

public:
 virtual int Add(System::Windows::Forms::DataGridViewRow ^ dataGridViewRow);
public virtual int Add (System.Windows.Forms.DataGridViewRow dataGridViewRow);
abstract member Add : System.Windows.Forms.DataGridViewRow -> int
override this.Add : System.Windows.Forms.DataGridViewRow -> int
Public Overridable Function Add (dataGridViewRow As DataGridViewRow) As Integer

Parameters

dataGridViewRow
DataGridViewRow

The DataGridViewRow to add to the DataGridViewRowCollection.

Returns

The index of the new DataGridViewRow.

Exceptions

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-

The DataGridView property of the dataGridViewRow is not null.

-or-

dataGridViewRow has a Selected property value of true.

-or-

This operation would add a frozen row after unfrozen rows.

dataGridViewRow is null.

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

Remarks

The Add(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