简体   繁体   English

从数据库中删除选定的数据表行

[英]Delete selected datatable row from database

I have a datatable in a DataGridView in C#. 我在C#中的DataGridView中有一个数据表。 I filled this table with values obtained from a MySql database. 我用从MySql数据库获得的值填充了该表。 Right now it can successfully retrieves the database table and adds to it once I added any new values to the datatable on WinForm side. 现在,一旦我在WinForm端向数据表添加了任何新值,它就可以成功检索数据库表并将其添加到数据库表中。 This is the code for this purpose: 这是用于此目的的代码:

dbDataset.RowChanged += new DataRowChangeEventHandler(Row_Changed); // dbDataset is my datatable name

and

    private static void Row_Changed(object sender, DataRowChangeEventArgs e)
    {
            // here sda is my MySqlDataAdapter
            try
            {
                MySqlCommandBuilder cmdb = new MySqlCommandBuilder(sda);
                dbDataset.GetChanges();
                dbDataset.GetChanges(DataRowState.Added);
                dbDataset.GetChanges(DataRowState.Deleted);
                dbDataset.GetChanges(DataRowState.Detached);
                dbDataset.GetChanges(DataRowState.Modified);
                dbDataset.GetChanges(DataRowState.Unchanged);                    
                sda.Update(dbDataset);                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
    }

But now I found that when I want to delete any row from the table, for instance I select a row and press "delete" key, it shows on the screen that it is deleted, but once I reload my WinForm, it still is there. 但是现在我发现,当我想从表中删除任何行时,例如,我选择一行并按“删除”键,它在屏幕上显示它已被删除,但是一旦我重新加载WinForm,它仍然存在。 Obviously it didn't delete that selected row from the database. 显然,它没有从数据库中删除所选的行。 For that, after some search, I added these parts: 为此,经过一些搜索,我添加了以下部分:

dbDataset.RowDeleted += new DataRowChangeEventHandler(Row_Deleted);

and

    private static void Row_Deleted(object sender, DataRowChangeEventArgs e)
    {
        dbDataset.AcceptChanges();
    }

By debugging, I found that when I press "delete" key, it enters the Row_Delete function, then to Row_Changed function, but throws an exception, saying: 通过调试,我发现当我按“删除”键时,它进入Row_Delete函数,然后进入Row_Changed函数,但是抛出异常,说:

Additional information: Concurrency violation: the DeleteCommand affected 0 of the expected 1 records.

I searched to get the current row index of selection, so that I could use DataRow.delete method like: 我进行搜索以获取选择的当前行索引,以便可以使用DataRow.delete方法,例如:

CurrencyManager xCM = = (CurrencyManager)this.BindingContext[this.dataGridView1.DataSource, this.dataGridView1.DataMember];

and in the Row_Deleted function I added: Row_Deleted函数中,我添加了:

DataRowView xDRV = (DataRowView)xCM.Current;
DataRow xDR = ((DataRowView)xCM.Current).Row;
xDR.Delete();

but it gives out null exception, saying the datasource of dataGridView1 cannot be null. 但它给出了null异常,表示dataGridView1的数据源不能为null。

Is there any idea for how to delete the selected row from database? 是否有关于如何从数据库中删除所选行的想法? Thanks in advance. 提前致谢。

I would use two distinct methods for the updates. 我将使用两种不同的方法进行更新。 One for the Insert/Update and one for the Delete. 一种用于插入/更新,另一种用于删除。
When you delete a Row the framework will call the RowDeleted event handler, not the RowChanged. 当您删除行时,框架将调用RowDeleted事件处理程序,而不是RowChanged。

private static void Row_Changed(object sender, DataRowChangeEventArgs e)
{
    try
    {
        MySqlCommandBuilder cmdb = new MySqlCommandBuilder(sda);
        sda.Update(dbDataset);                
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void Row_Deleted(object sender, DataRowChangeEventArgs e)
{
    try
    {
        SqlCommandBuilder cmdb = new SqlCommandBuilder(da);
        da.Update(dt);                
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

You should remove the AcceptChanges call. 您应该删除AcceptChanges调用。 According to MSDN 根据MSDN

When you call AcceptChanges on the DataSet, any DataRow objects still in edit-mode end their edits successfully. 当您在数据集上调用AcceptChanges时,任何仍处于编辑模式的DataRow对象都将成功结束其编辑。 The RowState property of each DataRow also changes; 每个DataRow的RowState属性也将更改; Added and Modified rows become Unchanged, and Deleted rows are removed. 添加和修改的行变为不变,删除的行被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM