DataGridViewRowCollection.InsertCopy(Int32, Int32) 方法


基于指定位置处的行,在集合中的指定位置插入行。Inserts a row into the collection at the specified position, based on the row at specified position.

 virtual void InsertCopy(int indexSource, int indexDestination);
public virtual void InsertCopy (int indexSource, int indexDestination);
abstract member InsertCopy : int * int -> unit
override this.InsertCopy : int * int -> unit
Public Overridable Sub InsertCopy (indexSource As Integer, indexDestination As Integer)



新行所基于的行的索引。The index of the row on which to base the new row.


要插入行的位置。The position at which to insert the row.


indexSource 小于零或大于集合中的行数减一。indexSource is less than zero or greater than the number of rows in the collection minus one.

- 或 --or- indexDestination 小于零或大于集合中的行数。indexDestination is less than zero or greater than the number of rows in the collection.

关联的 DataGridView 控件正在执行以下操作之一,该操作暂时禁止添加新行: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- 正在从以下 DataGridView 事件之一的处理程序调用此方法:This method is being called from a handler for one of the following DataGridView events:

-or- indexDestination 等于集合中的行数,且 AllowUserToAddRowstrueindexDestination is equal to the number of rows in the collection and AllowUserToAddRows is true.

- 或 --or- 此操作在未冻结的行之后插入一个冻结的行,或在冻结的行之前插入一个未冻结的行。This operation would insert a frozen row after unfrozen rows or an unfrozen row before frozen rows.


此方法将在indexDestination参数中指定的索引分配新行, 并递增所有后续行的索引。This method assigns the new row the index specified in the indexDestination parameter and increments the indexes for all subsequent rows. InheritedStyleInheritedStyle 的属性DataGridViewRow的值与定位于indexSource的的值相同。 DataGridViewRowThe InheritedStyle property of the new DataGridViewRow has the same values as the InheritedStyle of the DataGridViewRow positioned at indexSource. 如果可能DataGridViewRowCollection,方法会向中添加一个共享行。InsertCopyThe InsertCopy method adds a shared row to the DataGridViewRowCollection, if possible. 否则, 新行是不共享的。Otherwise, the new row is unshared. 有关详细信息,请参阅 缩放 Windows 窗体 DataGridView 控件的最佳做法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. 若要将新行排序到其正确位置, DataGridView.SortDataGridView.RowsAdded在事件处理程序中调用方法。To sort new rows into their correct position, call the DataGridView.Sort method in a DataGridView.RowsAdded event handler. 你可能还需要DataGridView.Sort CellValueChanged在事件处理程序中调用方法, 以便在用户修改单元格时对行进行排序。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.