【问题标题】:Data grid view CellValueChanged event throwing InvalidOperationExceptionDatagridview CellValueChanged 事件抛出 InvalidOperationException
【发布时间】:2017-01-27 05:03:14
【问题描述】:

当我更改单元格值以进行更新并直接单击菜单条项以打开新的 Winform 时,抛出 InvalidOperationException

   private void dgv_category_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            DataTable dt = new DataTable();
            dt = u.operationOnDataBase(sqlquery_selectCategory, 3);
            if (dt.Rows.Count > 0)
            {
                MessageBox.Show("Category Already Exist...");

            }
            else
            {
                u.operationOnDataBase(sqlquery_UpdateCategory, 1);
                u.SyncMaster("update", "CategoryDetails", 0, Convert.ToInt32(dgv_category[1, e.RowIndex].Value.ToString()));//---------Sync
            }

            try
            {
                dgv_category.DataSource = null; //here Throwing exception

                u.operationOnDataBase(sqlquery, 3);
                dgv_category.DataSource = u.dt;


            }
            catch (InvalidOperationException)
            {
                // exception
            }
        }

异常 - 操作无效,因为它会导致 可重入调用 SetCurrentCellAddressCore 函数。

在 System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex,Int32 rowIndex,布尔值 setAnchorCellAddress,布尔值 validateCurrentCell, Boolean throughMouseClick) 在 System.Windows.Forms.DataGridView.set_CurrentCell(DataGridViewCell 值)在 System.Windows.Forms.DataGridView.set_DataSource(对象 值)

【问题讨论】:

  • 你试过这个代码private void dgv_category_CellEndEdit(object sender, DataGridViewCellEventArgs e) { this.BeginInvoke(new MethodInvoker(() => }
  • @GovindTupkar 是的,我试过了,但没用...
  • 您尝试过 CellLeave 事件或 CellValidating 事件吗?
  • @FakeisMe,是的,我也尝试过 CellLeave 事件和 CellValidating 事件。

标签: c# .net winforms datagridview invalidoperationexception


【解决方案1】:

不直接设置DataSource,而是将DataSource设置为BindingSource,然后再更改BindingSource.DataSource?

例如

//create bindingSource in the WinForms Designer

那么……

try
{
   dgv_category.DataSource = null;
   dgv_category.Rows.Clear();
}
catch{}
bindingSource.DataSource = ut.dt;
dgv_category.DataSource = bindingSource;
bindingSource.ResetBindings(true);

【讨论】:

  • 我试过但没有工作它在dgv_category.DataSource = null;上抛出错误
  • 这就是 try/catch 块的原因。有时,您可以调用 DataSource = null 并且它会正常工作,有时它会引发错误。捕获并忽略这些异常可以让其余代码正常工作。
【解决方案2】:

试试这个

private void dgv_category_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        DataTable dt = new DataTable();
        dt = u.operationOnDataBase(sqlquery_selectCategory, 3);
        if (dt.Rows.Count > 0)
        {
            MessageBox.Show("Category Already Exist...");

        }
        else
        {
            u.operationOnDataBase(sqlquery_UpdateCategory, 1);
            u.SyncMaster("update", "CategoryDetails", 0, Convert.ToInt32(dgv_category[1, e.RowIndex].Value.ToString()));//---------Sync
        }

        try
        {
           /// dgv_category.DataSource = null; //you don't need to set it to null just remove it

            //try set empty data table 
            DataTable dt = new DataTable();
           dgv_category.DataSource = dt;

            u.operationOnDataBase(sqlquery, 3);
            dgv_category.DataSource = u.dt;


        }
        catch (InvalidOperationException)
        {
            // exception
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 2012-11-29
    • 2016-07-17
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    相关资源
    最近更新 更多