当我更改单元格值以进行更新时抛出InvalidOperationException
并直接单击菜单条项以打开新的Winform。
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
}
}
异常 - 操作无效,因为它导致a 可重入调用SetCurrentCellAddressCore函数。
在System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex,Int32 rowIndex,Boolean setAnchorCellAddress,Boolean validateCurrentCell,Boolean throughMouseClick)at System.Windows.Forms.DataGridView.set_CurrentCell(的DataGridViewCell System.Windows.Forms.DataGridView.set_DataSource(Object。) 值)
答案 0 :(得分:0)
不是直接设置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);
答案 1 :(得分:0)
尝试这个
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
}
}