有这样一种情形,在内存中对DataTable进行增行删行等操作后,最后要根据这些操作进行其他一些操作,比如对已删除的行,删除其他表中引用该行的记录。可是,当行被删除后,状态为Deleted,不能读取该行的数据了,那我如何获得哪些行被删除了呢?

解决方案 »

  1.   


                Dim tt As DataTable
                if tt.Rows(i).RowState = DataRowState.Deleted then
                    ......
      

  2.   

    tt.Rows(i).RowState = DataRowState.Deleted
      

  3.   

    那个行被删除知道,可是不能用myDataRow["列名"]获得该行的数据了呀
      

  4.   

    DataTable dt_D = dt.GetChanges(DataRowState.Deleted);
      

  5.   

    DataTable dt_D = dt.GetChanges(DataRowState.Deleted);
      

  6.   

    在.NET中,每行数据的变化在内存中都有记录,可以根据DataRow的State属性来查询出记录。可以使用
    DataTable dt=((DataTable)this.grdDetail.DataSource).GetChanges(DataRowState.Deleted);
    再使用dt.Select()方法,根据所给条件查询出所要的记录。
      

  7.   

    你们是不是不明白我问的是什么?如果某行的状态为deleted,那么就不能通过dr["列名"]获取该行某
    列的数据了,怎么办?
      

  8.   

    //获取不同版本的DataRow
    using System;
    using System.IO;
    using System.Text;
    using System.Diagnostics;
    using System.Threading;
    using System.Collections;
    using System.Data;
    using System.Xml;
    using System.Management;
    using System.Net;
    namespace Zhzuo
    {
    class ZZConsole
    {
    [STAThread]
    static void Main(string[] args)
    {
    DataSet ds = new DataSet();
    CreatDataSetSchema(ds);
    InitData(ds);
    DataRow drdel = ds.Tables["Hosts"].Rows[0];
    drdel.Delete();
    Console.WriteLine(drdel.HasVersion(DataRowVersion.Default).ToString());
    foreach(DataRow dr in ds.Tables["Hosts"].GetChanges(DataRowState.Deleted).Rows)
    {
    //if(dr.HasVersion(DataRowVersion.Current))
    //{
    Console.WriteLine((string)dr["HId"]);
    Console.WriteLine(dr["IsLocal",DataRowVersion.Original].ToString());
    //}
    }
    Console.WriteLine("end");
    Console.ReadLine();
    }
    //初始化数据集结构
    private static void CreatDataSetSchema(DataSet ds)
    {
    DataTable dt = new DataTable("Hosts");
    DataColumn dc = new DataColumn("HId",typeof(String));
    dt.Columns.Add(dc);
    dc = new DataColumn("IsLocal",typeof(Boolean));
    dt.Columns.Add(dc);
    ds.Tables.Add(dt);
    }
    //加入数据
    private static void InitData(DataSet ds)
    {
    DataRow hostsRow = ds.Tables["Hosts"].NewRow();
    hostsRow["HId"] = "192.192.132.229";
    hostsRow["IsLocal"] = true;
    ds.Tables["Hosts"].Rows.Add(hostsRow); hostsRow = ds.Tables["Hosts"].NewRow();
    hostsRow["HId"] = "192.192.132.231";
    hostsRow["IsLocal"] = false;
    ds.Tables["Hosts"].Rows.Add(hostsRow); hostsRow = ds.Tables["Hosts"].NewRow();
    hostsRow["HId"] = "192.192.132.233";
    hostsRow["IsLocal"] = false;
    ds.Tables["Hosts"].Rows.Add(hostsRow);
    }
    }

    }