DataGridViewRowCollection.InsertCopy(Int32, Int32) 方法

定義

根據位於指定位置的資料列,在指定的位置將資料列插入集合中。

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

參數

indexSource
Int32

當成新資料列基底的資料列索引。

indexDestination
Int32

要插入資料列的位置。

例外狀況

indexSource 小於零或大於集合中的資料列數目減一。

-或-

indexDestination 小於零或大於集合中的資料列數目。

關聯的 DataGridView 控制項正在執行下列其中一個動作,會暫時防止加入新的資料列:

  • 選取控件中的所有儲存格。

  • 清除選取範圍。

-或-

針對下列其中一個 DataGridView 事件 ,從處理常式呼叫這個方法:

-或-

indexDestination 等於集合中的資料列數目,且 AllowUserToAddRowstrue

-或-

這項作業會在未凍結的資料列之後插入凍結的資料列,或是在凍結的資料列之前插入未凍結的資料列。

備註

這個方法會將參數中指定的 indexDestination 索引指派給新的數據列,並遞增所有後續數據列的索引。 InheritedStyleDataGridViewRow 的 屬性具有與位於 indexSource之 的 相同值InheritedStyleDataGridViewRow。 如果可能的話,方法 InsertCopy 會將共享資料列加入至 DataGridViewRowCollection。 否則,不會共用新的數據列。 如需詳細資訊,請參閱 縮放 Windows Form DataGridView 控制項的最佳做法

新增數據列時,控件中的數據列不會自動排序。 若要將新數據列排序為正確的位置,請在事件處理程式中DataGridView.RowsAdded呼叫 DataGridView.Sort 方法。 您也可以在事件處理程式中CellValueChanged呼叫 DataGridView.Sort 方法,以在使用者修改儲存格時排序數據列。

適用於

另請參閱