DataGridViewRowCollection.Insert 方法

定义

向集合插入一行或多行。

重载

Insert(Int32, DataGridViewRow)

将指定的 DataGridViewRow 插入集合中。

Insert(Int32, Int32)

在集合的指定位置插入指定数量的行。

Insert(Int32, Object[])

在集合中的指定位置插入一行,并用指定的对象填充单元格。

Insert(Int32, DataGridViewRow)

Source:
DataGridViewRowCollection.cs
Source:
DataGridViewRowCollection.cs
Source:
DataGridViewRowCollection.cs

将指定的 DataGridViewRow 插入集合中。

C#
public virtual void Insert(int rowIndex, System.Windows.Forms.DataGridViewRow dataGridViewRow);

参数

rowIndex
Int32

要插入行的位置。

dataGridViewRow
DataGridViewRow

要插入到 DataGridViewRowCollectionDataGridViewRow

例外

rowIndex 小于零或大于集合中的行数。

dataGridViewRownull

关联的 DataGridView 控件正在执行以下操作之一,该操作暂时禁止添加新行:

  • 选择控件中的所有单元格。

  • 清除所选内容。

- 或 -

正在从以下 DataGridView 事件之一的处理程序调用此方法:

- 或 -

DataGridViewDataSource 属性不是 null

- 或 -

rowIndex 等于集合中的行数,并且 DataGridViewAllowUserToAddRows 属性设置为 true

- 或 -

DataGridView 没有列。

- 或 -

dataGridViewRowDataGridView 属性不是 null

- 或 -

dataGridViewRow 具有 trueSelected 属性值。

- 或 -

此操作将在未冻结的行之后插入一个冻结的行,或在冻结的行之前插入一个未冻结的行。

dataGridViewRow 包含的单元格比控件中的列多。

注解

如果 Insert(Int32, DataGridViewRow) 可能, 方法将共享行添加到 DataGridViewRowCollection。 否则,新行是非共享的。 有关详细信息,请参阅 缩放 Windows 窗体 DataGridView 控件的最佳做法

添加新行时,控件中的行不会自动排序。 若要将新行排序到正确的位置,请在事件处理程序中DataGridView.RowsAdded调用 DataGridView.Sort 方法。 用户修改单元格时,可能还需要在事件处理程序中CellValueChanged调用 DataGridView.Sort 方法来对行进行排序。

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Insert(Int32, Int32)

Source:
DataGridViewRowCollection.cs
Source:
DataGridViewRowCollection.cs
Source:
DataGridViewRowCollection.cs

在集合的指定位置插入指定数量的行。

C#
public virtual void Insert(int rowIndex, int count);

参数

rowIndex
Int32

要插入行的位置。

count
Int32

要在 DataGridViewRowCollection 中插入的行数。

例外

rowIndex 小于零或大于集合中的行数。

- 或 -

count 小于 1。

关联的 DataGridView 控件正在执行以下操作之一,该操作暂时禁止添加新行:

  • 选择控件中的所有单元格。

  • 清除所选内容。

- 或 -

正在从以下 DataGridView 事件之一的处理程序调用此方法:

- 或 -

DataGridViewDataSource 属性不是 null

- 或 -

DataGridView 没有列。

- 或 -

rowIndex 等于集合中的行数,并且 DataGridViewAllowUserToAddRows 属性设置为 true

- 或 -

RowTemplate 属性返回的行包含的单元格比控件中的列多。

- 或 -

此操作将在未冻结的行之后插入一个冻结的行,或在冻结的行之前插入一个未冻结的行。

注解

方法 Insert(Int32, Int32) 添加基于 RowTemplate 的 的 DataGridView行。 如果可能,将共享新行。 确保可以共享 属性中指定的 RowTemplate 行,以获得最佳可伸缩性。 有关详细信息,请参阅 缩放 Windows 窗体 DataGridView 控件的最佳做法

添加新行时,控件中的行不会自动排序。 若要将新行排序到正确的位置,请在事件处理程序中DataGridView.RowsAdded调用 DataGridView.Sort 方法。 用户修改单元格时,可能还需要在事件处理程序中CellValueChanged调用 DataGridView.Sort 方法来对行进行排序。

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Insert(Int32, Object[])

Source:
DataGridViewRowCollection.cs
Source:
DataGridViewRowCollection.cs
Source:
DataGridViewRowCollection.cs

在集合中的指定位置插入一行,并用指定的对象填充单元格。

C#
public virtual void Insert(int rowIndex, params object[] values);

参数

rowIndex
Int32

要插入行的位置。

values
Object[]

用于填充新行的单元格的对象变量数。

例外

rowIndex 小于零或大于集合中的行数。

valuesnull

关联的 DataGridView 控件正在执行以下操作之一,该操作暂时禁止添加新行:

  • 选择控件中的所有单元格。

  • 清除所选内容。

- 或 -

正在从以下 DataGridView 事件之一的处理程序调用此方法:

- 或 -

DataGridViewVirtualMode 属性设置为 true

- 或 -

DataGridViewDataSource 属性不是 null

- 或 -

DataGridView 没有列。

- 或 -

rowIndex 等于集合中的行数,并且 DataGridViewAllowUserToAddRows 属性设置为 true

- 或 -

由控件的 RowTemplate 属性返回的行的 DataGridView 属性不是 null

- 或 -

此操作将在未冻结的行之后插入一个冻结的行,或在冻结的行之前插入一个未冻结的行。

控件的 RowTemplate 属性返回的行包含的单元格比控件中的列多。

注解

方法Insert(Int32, Object[])将非共享行添加到 。DataGridViewRowCollection

添加新行时,控件中的行不会自动排序。 若要将新行排序到正确的位置,请在事件处理程序中DataGridView.RowsAdded调用 DataGridView.Sort 方法。 用户修改单元格时,可能还需要在事件处理程序中CellValueChanged调用 DataGridView.Sort 方法来对行进行排序。

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10