我是这样做的
DataRow dr=ds.Tables[0].Rows.NewRow();
dr["id"]=12;
dr["name"]="wo";
ds.Tables[0].Rows.Remove(dr);]
提示错误"给定数据行不在当前的DataRowCollection中"

解决方案 »

  1.   

    DataRow dr=ds.Tables[0].Rows[0];ds.Tables[0].Rows.Remove(dr);
    移除了第1行
      

  2.   

    DataRow dr=ds.Tables[0].Rows.NewRow();
    dr["id"]=12;
    dr["name"]="wo";
    这样确定的
      

  3.   

    DataRow dr=ds.Tables[0].Rows[12];ds.Tables[0].Rows.Remove(dr);这就把ID=12的行给移了,主键要是ID
      

  4.   

    在dataset中先搜索到你要找到的那個行,把下標取出來,然後根據下標刪除那個行不就行了么!
      

  5.   

    ds.Tables[0].PrimaryKey = new DataColumn[]{ds.Tables[0].Columns["id"]};
     ds.Tables[0].Rows.Find(id).Delete();
    根据你指定的id删除一行
      

  6.   

    DataRow dr=ds.Tables[0].Rows[index];ds.Tables[0].Rows.Remove(dr);
      

  7.   

    DataRow dr=ds.Tables[0].NewRow();
    dr["id"]=12;
    dr["name"]="wo";
    ds.Tables[0].Rows.Remove(dr);
    我是这样做的,我的是没错,你可以试一下,要是还有错,就是SQL的问题拉
      

  8.   

    感谢zhangkunls(小憨)
    你的方法在我这里可行
    可是我不明白为什末 fanliang11(以编程为兴趣,以盖茨为激励) 
    和我同样的方法他却没有出错
      

  9.   

    heyidan(gopee)
    你说的挺有道理
      

  10.   

    Sample code as follows:
    DataRow dr=ds.Tables[0].NewRow();
    dr["id"]=12;
    dr["name"]="wo";
    ds.Tables[0].Rows.Add( dr );
    ds.Tables[0].Rows.Remove(dr);
      

  11.   

    区分DataTable.NewRow和DataRowCollection.Add两个方法的区别。前者是用当前的Table结构产生一个新的DataRow,但是此DataRow并没有加载到DataTable.Rows中。后者相当于把一个DataRow加载到DataTable.Rows中,这样一个新建的DataRow才属于此Rows集合中。
      

  12.   

    可以试试用下面的代码:DataSet ds = new Dataset();
    DataTable table = new DataTable("database");
    table.Columns.Add("ID",typeof(int));
    table.Columns.Add("Name",typeof(string));
    ds.Tables.Add(table);DataRow row = table.NewRow();
    row["ID"] = 12;
    row["Name"]="wo";
    table.Rows.Add(row);