dataGridView1_RowsRemoved无法正常工作 [英] dataGridView1_RowsRemoved not working as I expect

查看:232
本文介绍了dataGridView1_RowsRemoved无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码加载我的Windows窗体:

I have the following code that load my windows form:

    private void Panou_Load(object sender, EventArgs e)
    {
        List<string>[] list;

        list = Conexiune.Select();
        dataGridView1.Rows.Clear();

        (dataGridView1.Columns[3] as DataGridViewComboBoxColumn).DataSource = new List<string> { "", "activ", "inactiv", "neverificat", "blocat" };

        for (int i = 0; i < list[0].Count; i++)
        {
            int number = dataGridView1.Rows.Add();
            dataGridView1.Rows[number].Cells[0].Value = list[0][i];
            dataGridView1.Rows[number].Cells[1].Value = list[1][i];
            dataGridView1.Rows[number].Cells[2].Value = list[2][i];
            dataGridView1.Rows[number].Cells[3].Value = list[3][i];
            dataGridView1.Rows[number].Cells[4].Value = list[4][i];
            dataGridView1.Rows[number].Cells[5].Value = list[5][i];
            dataGridView1.Rows[number].Cells[6].Value = list[6][i];
        }
    }

一切正常,但现在我要进行一些更新在数据库中删除行时使用 dataGridView1_RowsRemoved

Everything works normally but now I want to make some updates in database when I remove a row so I use: dataGridView1_RowsRemoved .

示例:

private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
    MessageBox.Show("deleted");
}




  1. 为什么消息已删除在加载表单时显示?

  1. Why the message "deleted" is showing up when the form load?

dataGridView1.Rows [number] .Cells [0] .Value 包含数据库中的ID。如何在 dataGridView1_RowsRemoved 函数中检索此ID?

dataGridView1.Rows[number].Cells[0].Value contain the id from database. How I retrieve this id in the dataGridView1_RowsRemoved function?


推荐答案

数据源(您的列表)到 DataGridView 的实际绑定发生在表单加载事件之后,因此发生了很多事件

The actual binding of the data source (your list) to the DataGridView happens after the form load event, and so a lot of events that your would not expect to be fired are fired in the process of data binding.

我不知道为什么会被解雇-您需要深入研究代码或 DataGridView 组件本身,但是幸运的是可以解决-在 DataBindingComplete 事件的事件处理程序期间附加所有此类事件

I don't know why that is - would need to dig through the code or the DataGridView component itself, but there is fortunately a work around - attach all such events during the event handler of the DataBindingComplete event.

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    dataGridView1.RowsRemoved += new DataGridViewRowsRemovedEventHandler(dataGridView1_RowsRemoved);        
}

那样,您将不会看到 RowsRemoved 在表单加载时触发。

That way you will not see RowsRemoved fired at form load.

更好的情况是,问题第二部分的答案涉及使用不同的事件,其行为类似于预期,即使在 DataBindingComplete 期间未附加。

Even better in your case, the answer to the second part of your question involves using a different event, that behaves as expected, even when not attached during DataBindingComplete.

RowsRemoved的问题是在删除该行之后将其触发,因此即使您具有事件args的行的(旧)索引,获取数据也非常棘手(也许不可能?)

The problem with RowsRemoved is that it fires after the row has been removed, so even though you have the (old) index of the row from the event args, getting to the data is very tricky (maybe impossible?)

解决方法是改为处理 UserDeletingRow 事件:

The fix is to instead handle the UserDeletingRow event:

void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
    var msg = e.Row.Cells[0].Value.ToString();

    MessageBox.Show(msg);
} 

对于删除的每一行都会触发一次此事件-可能会使合理地聚集这些事件,然后从 RowsRemoved 处理程序中执行单个数据库操作。

This event is fired once for every row that is deleted - it would probably make sense to aggregate these events and then perform a single database action from within the RowsRemoved handler.

这篇关于dataGridView1_RowsRemoved无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆