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日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分