DataGridViewRowCollection.AddCopies(Int32, Int32) 方法

定义

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

public:
 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

参数

indexSource
Int32

新行将以其为基础的行的索引。The index of the row on which to base the new rows.

count
Int32

要添加到 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.

注解

DataGridViewRowInheritedStyle 属性的值与位于 indexSourceDataGridViewRow InheritedStyle 的值相同。The InheritedStyle property of the new DataGridViewRow has the same values as the InheritedStyle of the DataGridViewRow positioned at indexSource. 如果可能,AddCopies 方法会将共享行添加到 DataGridViewRowCollection中。The 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.RowsAdded 事件处理程序中调用 DataGridView.Sort 方法。To sort new rows into their correct position, call the DataGridView.Sort method in a DataGridView.RowsAdded event handler. 你可能还需要在 CellValueChanged 事件处理程序中调用 DataGridView.Sort 方法,以便在用户修改单元格时对行进行排序。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.

适用于

另请参阅