在维护 DataGridView 上的多个表时处理事件的方法
本文介绍了一种在 DataGridView 上维护多个表时处理事件的方法。
引言
本文介绍了一个 C# 示例,展示了如何在维护多个表时添加/删除以及启动/停止事件处理程序。
背景
我在我的 C#.NET 项目中遇到了许多在 DataGridView 中维护多个表时的事件处理问题。 我无法找到所有问题的答案,但我希望在这里解释我为以下问题找到的解决方案:
- 何时移除先前表的事件处理程序
- 如何设置添加/删除或启动/停止事件处理程序的条件
- 何时添加/删除或启动/停止事件处理程序
使用代码
当您单击表单上的任何主表菜单时,它会在 DataGridView
上显示表记录并加载预期的事件,例如,在我的例子中,DataRowChange
事件处理程序。 首先,您需要卸载(移除)先前主表拥有的事件处理程序。 是否卸载 DataError
处理程序取决于您是否忽略先前表上的插入/更新。 在我的例子中,我忽略了它,因此我从 UnloadEventHandler()
中移除了 DataError
处理程序。 相反,我通过设置标志来停止处理程序的处理并重新启动处理。
private void CompanyToolStripMenuItem_Click(object sender, EventArgs e)
{
// Unload Previous Master Table's EventHandler
UnloadEventHandler();
// Set Current Master Table
currentMasterTable = "M_COMPANY";
// Get MCompany Table Records
MCompany company = new MCompany();
MasterMaintenanceBL mmbl = new MasterMaintenanceBL();
dataset = mmbl.MCompanyGetData(false);
// Display on DataGridView
company.DispCompanyData(dataset, this.dataGridView);
// Load RowChanging Event
RowChangingIsOn = true; // Load/Unlaod Flag
dataset.Tables["M_COMPANY"].RowChanging +=
new DataRowChangeEventHandler(MCompanyTable_RowChanging);
// Keep DataError EventHandler's Processing
StopProcessingDataErrorIsOn = false;
}
我将停止处理插入到 DataError
处理程序中,如下所示:
private void DataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
// When Table Loads, Stop Processing
if (StopProcessingDataErrorIsOn)
{
return;
}
if (e.Exception.Message.Equals(DgvDataErrorMessage.NotValidDataGridViewComboBoxCell))
{
// Your Code here
return;
}
else if(e.Exception.Message.Equals (DgvDataErrorMessage.CancelAdd))
{
// Your Code here
return;
}
else if (e.Exception.Message.Equals(DgvDataErrorMessage.CancelChange))
{
// Your Code here
return;
}
else if (e.Exception.Message.Equals(DgvDataErrorMessage.NullCompanyCD"))
{
// Your Code here
return;
}
else if (e.Exception.Message.Contains(DgvDataErrorMessage.ViloatePrimaryKey))
{
// Your Code here
return;
}
}
您可以使用标志卸载事件处理程序,如下所示:
private void UnloadEventHandler()
{
// Unload RowChanging EventHandler
if (RowChangingIsOn)
{
switch (currentMasterTable)
{
case "M_COMPANY":
dataset.Tables["M_COMPANY"].RowChanging -=
new DataRowChangeEventHandler(MCompanyTable_RowChangingConfirm);
break;
default:
break;
}
RowChangingIsOn = false;
}
// Unload SelectionChanged EventHandler
if (DataGridViewSelectionChangedIsOn)
{
this.dataGridView.SelectionChanged -=
new System.EventHandler(this.DataGridView_SelectionChanged);
DataGridViewSelectionChangedIsOn = false;
}
// Unload ComboBox_Validating EventHandler
DataGridViewComboEditBoxCell comboEditBoxCell =
this.dataGridView.CurrentCell as DataGridViewComboEditBoxCell;
if (comboEditBoxCell != null)
{
// If Validating EventHandler Loaded
if (comboEditBoxCell.GetComboBoxValidatingIsOn())
{
ComboBox comboBox = this.dataGridView.EditingControl as ComboBox;
if (comboBox != null)
{
comboBox.Validating -=
new CancelEventHandler(comboEditBoxCell.comboBox_Validating);
}
}
}
// Stop DataError EventHandler's Processing
StopProcessingDataErrorIsOn = true;
}
关注点
解决我们遇到的事件处理问题有很多方法。 在本文中,我希望我的示例能帮助您找到自己的解决方案。