DataRow.Delete 方法

删除 DataRow

**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Public Sub Delete
用法
Dim instance As DataRow

instance.Delete
public void Delete ()
public:
void Delete ()
public void Delete ()
public function Delete ()

异常

异常类型 条件

DeletedRowInaccessibleException

DataRow 已被删除。

备注

如果行的 RowState 是“Added”,则该行将从表中移除。

在使用 Delete 方法后,RowState 变成“Deleted”。在您调用 AcceptChanges 之前,它一直保持“已删除”。

可通过调用 RejectChanges 取消删除行。

示例

下面的示例创建一个简单的 DataTable,该表有两列、十行。在用 Delete 方法删除几个 DataRow 项之后,通过调用 RejectChanges 取消删除其中的一行。

 Private Sub DemonstrateDeleteRow()
     ' Create a simple DataTable with two columns and ten rows.
     Dim table As New DataTable("table")
     Dim idColumn As New DataColumn("id", Type.GetType("System.Int32"))
     idColumn.AutoIncrement = True
     Dim itemColumn As New DataColumn("item", Type.GetType("System.String"))
     table.Columns.Add(idColumn)
     table.Columns.Add(itemColumn)

     ' Add ten rows.
     Dim newRow As DataRow
     
     Dim i As Integer
     For i = 0 To 9
         newRow = table.NewRow()
         newRow("item") = "Item " & i.ToString()
         table.Rows.Add(newRow)
     Next i
     table.AcceptChanges()

     Dim itemColumns As DataRowCollection = table.Rows
     itemColumns(0).Delete()
     itemColumns(2).Delete()
     itemColumns(3).Delete()
     itemColumns(5).Delete()
     Console.WriteLine(itemColumns(3).RowState.ToString())

     ' Reject changes on one deletion.
     itemColumns(3).RejectChanges()

     ' Change the value of the column so it stands out.
     itemColumns(3)("item") = "Deleted, Undeleted, Edited"

     ' Accept changes on others.
     table.AcceptChanges()

     ' Print the remaining row values.
     Dim row As DataRow
     For Each row In  table.Rows
         Console.WriteLine(row(0).ToString() & ControlChars.Tab _
            & row(1).ToString())
     Next row
End Sub
private void DemonstrateDeleteRow()
{
    // Create a simple DataTable with two columns and ten rows.
    DataTable table = new DataTable("table");
    DataColumn idColumn = new DataColumn("id",
        Type.GetType("System.Int32"));
    idColumn.AutoIncrement=true;
    DataColumn itemColumn = new DataColumn("item", 
        Type.GetType("System.String"));
    table.Columns.Add(idColumn);
    table.Columns.Add(itemColumn);

    // Add ten rows.
    DataRow newRow;
     
    for(int i = 0; i <10; i++)
    {
        newRow = table.NewRow();
        newRow["item"] = "Item " + i;
        table.Rows.Add(newRow);
    }
    table.AcceptChanges();

    DataRowCollection itemColumns = table.Rows;
    itemColumns[0].Delete();
    itemColumns[2].Delete();
    itemColumns[3].Delete();
    itemColumns[5].Delete();
    Console.WriteLine(itemColumns[3].RowState.ToString());

    // Reject changes on one deletion.
    itemColumns[3].RejectChanges();

    // Change the value of the column so it stands out.
    itemColumns[3]["item"] = "Deleted, Undeleted, Edited";

    // Accept changes on others.
    table.AcceptChanges();

    // Print the remaining row values.
    foreach(DataRow row in table.Rows)
    {
        Console.WriteLine(row[0] + "\table" + row[1]);
    }
}

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

DataRow 类
DataRow 成员
System.Data 命名空间
DataRowState
DataViewRowState
RowState