Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在CellEndEdit上重新填充DataGridView_C#_Winforms_Datagridview - Fatal编程技术网

C# 在CellEndEdit上重新填充DataGridView

C# 在CellEndEdit上重新填充DataGridView,c#,winforms,datagridview,C#,Winforms,Datagridview,我试图在用户编辑完单元格后更新数据库中的值。由于数据绑定此特定数据集涉及的一些复杂性,DataGridView是手动(动态)填充的 在CellEndEdit的事件处理程序中,我调用了我的方法将值更新回数据库,它按计划工作。当我尝试重新填充DataGridView时,就会出现问题。作为参考,此方法称为PopulateAccounts() 在单元格中输入值后,如果用户按键盘上的Enter键或单击窗体上的另一个控件,则一切都会正常工作。但是,单击同一DataGridView中的另一个单元格会导致以下错

我试图在用户编辑完单元格后更新数据库中的值。由于数据绑定此特定数据集涉及的一些复杂性,DataGridView是手动(动态)填充的

CellEndEdit
的事件处理程序中,我调用了我的方法将值更新回数据库,它按计划工作。当我尝试重新填充DataGridView时,就会出现问题。作为参考,此方法称为
PopulateAccounts()

在单元格中输入值后,如果用户按键盘上的Enter键或单击窗体上的另一个控件,则一切都会正常工作。但是,单击同一DataGridView中的另一个单元格会导致以下错误:

操作无效,因为它导致对SetCurrentCellAddressCore函数的可重入调用

PopulateAccounts()
方法包括引发错误的
DataGridView.Rows.Clear()
。我在几个相关的SO问题中研究了这个错误,它似乎与线程有关,我对此一无所知。一个建议的修复方法是调用我的
PopulateAccounts()
方法:

BeginInvoke(new MethodInvoker(PopulateAccounts));
这是可行的,但它会导致DataGridView选择已编辑单元格之前的所有单元格(请参见下面的屏幕截图)

同样,这种仅在单击单元格外并单击另一单元格时发生。否则,即按Enter键或单击另一个控件,它只选择第一个单元格

以下是PopulateAccounts()的代码供参考:

    // Populates the DataGridView's account records.
    private void PopulateAccounts()
    {
        dgvShed.Rows.Clear();

        using (PassShedEntities context = new PassShedEntities(conString))
        {
            foreach (Account acct in context.Account.Where(a => a.Category_id == (int)lstCategories.SelectedValue))
            {
                dgvShed.Rows.Add();

                foreach (DataGridViewColumn col in dgvShed.Columns)
                {
                    Credential cred = (from c in context.Credential
                                       join f in context.Field on c.Field_id equals f.Id
                                       join a in context.Account on c.Account_id equals a.Id
                                       where c.Account_id == acct.Id
                                       where f.Label == col.Name
                                       select c).SingleOrDefault();

                    dgvShed.Rows[dgvShed.Rows.Count - 1].Cells[col.Name].Tag = cred.Id;
                    dgvShed.Rows[dgvShed.Rows.Count - 1].Cells[col.Name].Value = cred.Value;
                }
            }
        }
    }
一旦在单元格中输入了一个值,如果用户按键盘上的Enter键或单击窗体上的另一个控件,一切都会正常工作

上述操作不会更改
CurrentCell
,网格重新填充工作正常

但是,单击同一DataGridView中的另一个单元格会导致以下错误:

单击另一个单元格时,datagridview的
CurrentCell
正在更改,网格内部调用
SetCurrentCellAddressCore
来更改单元格。此调用在内部引发
CellEndEdit
事件,该事件再次触发
SetCurrentCellAddressCore
,此循环继续,导致无限循环。
DataGridView
检测此循环并引发异常,导致您提到的错误


在上面的例子中,我的猜测是
DataGridView
不知怎么搞砸了单元格的选择。您只需使用
DataGridView.ClearSelection()
方法清除
PopulateAccounts
末尾的选择即可。

我一直在努力解决几乎完全相同的问题,刚刚想出了一个符合我的具体情况的解决方案。我将所有代码从CellEndEdit事件移动到CellValueChanged事件(在前者之前触发),现在结束单元格编辑的两个选项:输入笔划并单击另一个单元格,效果很好


我不确定什么是使用事件的最佳方式,但这似乎是有效的

你能在你的帖子中添加PopulateAccounts代码吗?完成。dgvShed.Rows.Clear()方法抛出错误。很抱歉,我忘记指定在PopulateAccounts()方法之后尝试使用ClearSelection(),它根本没有效果。由于CellEndEdit更新了数据库,我意识到我不一定需要在这个事件处理程序中重新填充DataGridView,因为单元格已经包含了与数据库完全相同的值(从技术上讲,它没有绑定到数据库)。所以,除非有人知道一种方法来解决细胞选择的问题,这对我来说已经不是问题了。