gpt4 book ai didi

c# - Datagridview CellValueChanged 事件抛出 InvalidOperationException

转载 作者:太空狗 更新时间:2023-10-29 22:00:10 24 4
gpt4 key购买 nike

当我更改要更新的单元格值并直接单击菜单条项以打开新的 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
}
}

Exception- Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick) at System.Windows.Forms.DataGridView.set_CurrentCell(DataGridViewCell value) at System.Windows.Forms.DataGridView.set_DataSource(Object value)

最佳答案

不是直接设置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);

关于c# - Datagridview CellValueChanged 事件抛出 InvalidOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39574311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com