DataTableCollection.RemoveAt(Int32) Метод
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Удаляет объект DataTable с указанным индексом из коллекции.
public:
void RemoveAt(int index);
public void RemoveAt (int index);
member this.RemoveAt : int -> unit
Public Sub RemoveAt (index As Integer)
- index
- Int32
Индекс удаляемого объекта DataTable
.
В коллекции нет таблицы по указанному индексу.
В следующем примере используются Contains методы и CanRemove для проверки наличия таблицы с индексом 10. В этом случае RemoveAt вызывается метод для удаления таблицы.
public static void DataTableCollectionRemoveAt()
{
// Create a DataSet with two tables and then
// remove the tables from the collection using RemoveAt.
DataSet dataSet = new DataSet();
// Create Customer table.
DataTable customerTable = new DataTable("Customers");
customerTable.Columns.Add("customerId",
typeof(int)).AutoIncrement = true;
customerTable.Columns.Add("name", typeof(string));
customerTable.PrimaryKey = new DataColumn[] { customerTable.Columns["customerId"] };
// Create Orders table.
DataTable ordersTable = new DataTable("Orders");
ordersTable.Columns.Add("orderId",
typeof(int)).AutoIncrement = true;
ordersTable.Columns.Add("customerId", typeof(int));
ordersTable.Columns.Add("amount", typeof(double));
ordersTable.PrimaryKey = new DataColumn[] { ordersTable.Columns["orderId"] };
dataSet.Tables.AddRange(new DataTable[] { customerTable, ordersTable });
// Remove all tables.
// First check to see if the table can be removed,
// then remove it.
//
// You cannot use a foreach when removing items
// from a collection.
while (dataSet.Tables.Count > 0)
{
DataTable table = dataSet.Tables[0];
if (dataSet.Tables.CanRemove(table))
dataSet.Tables.RemoveAt(0);
Console.WriteLine("dataSet has {0} tables",
dataSet.Tables.Count);
}
}
Public Sub DataTableCollectionRemoveAt()
' Create a DataSet with two tables and then
' remove the tables from the collection using RemoveAt.
Dim dataSet As New DataSet()
' Create Customer table.
Dim customerTable As New DataTable("Customers")
customerTable.Columns.Add("customerId", _
GetType(Integer)).AutoIncrement = True
customerTable.Columns.Add("name", GetType(String))
customerTable.PrimaryKey = New DataColumn() _
{customerTable.Columns("customerId")}
' Create Orders table.
Dim ordersTable As New DataTable("Orders")
ordersTable.Columns.Add("orderId", _
GetType(Integer)).AutoIncrement = True
ordersTable.Columns.Add("customerId", GetType(Integer))
ordersTable.Columns.Add("amount", GetType(Double))
ordersTable.PrimaryKey = New DataColumn() _
{ordersTable.Columns("orderId")}
dataSet.Tables.AddRange(New DataTable() _
{customerTable, ordersTable})
' Remove all tables.
' First check to see if the table can be removed,
' then remove it.
'
' You cannot use a foreach when removing items
' from a collection.
Do While (dataSet.Tables.Count > 0)
Dim table As DataTable = dataSet.Tables(0)
If (dataSet.Tables.CanRemove(table)) Then
dataSet.Tables.RemoveAt(0)
End If
Console.WriteLine("dataSet has {0} tables", _
dataSet.Tables.Count)
Loop
End Sub
Это CollectionChanged событие возникает при успешном удалении таблицы.
Продукт | Версии |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 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 |
.NET Standard | 2.0, 2.1 |
Отзыв о .NET
.NET — это проект с открытым исходным кодом. Выберите ссылку, чтобы оставить отзыв: