DataGridViewRowCollection.AddCopies(Int32, Int32) 方法


基于指定索引处的行在集合中添加指定数目的行。Adds the specified number of rows to the collection based on the row at the specified index.

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



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


要添加到 DataGridViewRowCollection 的行数。The number of rows to add to the DataGridViewRowCollection.


已添加的最后一行的索引。The index of the last row that was added.


indexSource 小于零或大于等于控件中的行数。indexSource is less than zero or greater than or equal to the number of rows in the control.

-or- count 小于零。count is less than zero.

关联的 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- DataSourceDataGridView 属性不是 nullThe DataSource property of the DataGridView is not null.

-or- 此操作在未冻结的行之后添加一个冻结的行。This operation would add a frozen row after unfrozen 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,方法会将共享行添加到中。AddCopiesThe AddCopies method adds shared rows to the DataGridViewRowCollection, if possible. 否则, 新行将取消共享。Otherwise, the new rows are 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.