Coisini_1ge 2022-03-03 17:40 采纳率: 100%
浏览 54
已结题

System.InvalidOperationException:“当传递具有已删除行的 DataRow 集合时,Update 要求有效的 DeleteCommand。”

问题遇到的现象和发生背景

img

问题相关代码,请勿粘贴截图

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ck
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}

    public void FrmMain_Load(object sender, EventArgs e)
    {
        // TODO: 这行代码将数据加载到表“ckDataSet.Goods”中。您可以根据需要移动或删除它。
        this.GoodsTableAdapter.Fill(this.ckDataSet.Goods);
        

    }

    public void btnQuery_Click(object sender, EventArgs e)
    {
        string goodsNameStr = txtQuery.Text;
        if (goodsNameStr != "")
        {
            dgvGoods.DataSource = ckDataSet.Goods.Select("GoodsName Like\'" + goodsNameStr + "%\'");
        }
        else
        {
            dgvGoods.DataSource = ckDataSet.Goods;
        }
    }

    public void btnSelect_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("确定要删除此行数据吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {
            DataRow delrow = ckDataSet .Goods .Rows[dgvGoods.SelectedRows[0].Index];
            delrow.Delete();
            GoodsTableAdapter.Update (ckDataSet.Goods);
            ckDataSet.Goods.AcceptChanges();
        }

    }
}

}

运行结果及报错内容

System.InvalidOperationException:“当传递具有已删除行的 DataRow 集合时,Update 要求有效的 DeleteCommand。”

我的解答思路和尝试过的方法
我想要达到的结果

这个怎么解决吖,我照着书上写的但是我一运行就报错,初学者,希望能够帮忙指点指点

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-03-03 18:08
    关注

    测试可用的简单示例如下

    img

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.SqlClient;
    
    namespace WindowsFormsApp8
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                conn = new SqlConnection("server=.;uid=test;pwd=123456;database=test;");
                Goods =  new DataTable();
                GoodsTableAdapter = new SqlDataAdapter("select * from goods", conn);
                thisBuilder = new SqlCommandBuilder(GoodsTableAdapter);
    
    
            }
            SqlCommandBuilder thisBuilder ;
            DataTable Goods;
            SqlDataAdapter GoodsTableAdapter;
            SqlConnection conn;
            public void Form1_Load(object sender, EventArgs e)
            {
                GoodsTableAdapter.Fill(Goods);
    
    
            }
    
            public void btnQuery_Click(object sender, EventArgs e)
            {
                string goodsNameStr = txtQuery.Text;
                if (goodsNameStr != "")
                {
                    dgvGoods.DataSource = Goods.Select("GoodsName Like\'" + goodsNameStr + "%\'");
                }
                else
                {
                    dgvGoods.DataSource = Goods;
                }
            }
    
            public void btnSelect_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("确定要删除此行数据吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    DataRow delrow = Goods.Rows[dgvGoods.SelectedRows[0].Index];
                    delrow.Delete();
                    GoodsTableAdapter.Update(Goods);
                    Goods.AcceptChanges();
                }
    
            }
        }
    }
    
    
    

    img


    有其他问题可以继续交流~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 3月11日
  • 已采纳回答 3月3日
  • 创建了问题 3月3日

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services