Introduction
This article introduces a C# sample which shows how to add/remove and start/stop an event handler when you maintain many tables in a DataGridView
.
Background
I have experienced many event handling problems in my C#.NET project which maintains many tables in a DataGridView
. I couldn't find out the answer to all my problems, but I wish to explain here the solution that I have found for the following problems:
- When to remove the previous table's event handler
- How to set the condition to add/remove or start/stop an event handler
- When to add/remove or start/stop an event handler
Using the code
When you click any master table menu on a form, it shows the table records on the DataGridView
and loads the expected events, for example, in my case the DataRowChange
event handler. At first, you need to unload (remove) the event handlers that the previous master table had. Whether you unload the DataError
handler or not depends on whether you ignore the insert/update on the previous table. In my case, I ignored it so that I got rid of the DataError
handler from UnloadEventHandler()
. Instead, I stopped the processing of the handler and restarted the processing by setting the flag.
private void CompanyToolStripMenuItem_Click(object sender, EventArgs e)
{
UnloadEventHandler();
currentMasterTable = "M_COMPANY";
MCompany company = new MCompany();
MasterMaintenanceBL mmbl = new MasterMaintenanceBL();
dataset = mmbl.MCompanyGetData(false);
company.DispCompanyData(dataset, this.dataGridView);
RowChangingIsOn = true;
dataset.Tables["M_COMPANY"].RowChanging +=
new DataRowChangeEventHandler(MCompanyTable_RowChanging);
StopProcessingDataErrorIsOn = false;
}
I inserted stop-processing into the DataError
handler as follows:
private void DataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
if (StopProcessingDataErrorIsOn)
{
return;
}
if (e.Exception.Message.Equals(DgvDataErrorMessage.NotValidDataGridViewComboBoxCell))
{
return;
}
else if(e.Exception.Message.Equals (DgvDataErrorMessage.CancelAdd))
{
return;
}
else if (e.Exception.Message.Equals(DgvDataErrorMessage.CancelChange))
{
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;
}
}
You can unload the event handlers using the flag as follows:
private void UnloadEventHandler()
{
if (RowChangingIsOn)
{
switch (currentMasterTable)
{
case "M_COMPANY":
dataset.Tables["M_COMPANY"].RowChanging -=
new DataRowChangeEventHandler(MCompanyTable_RowChangingConfirm);
break;
default:
break;
}
RowChangingIsOn = false;
}
if (DataGridViewSelectionChangedIsOn)
{
this.dataGridView.SelectionChanged -=
new System.EventHandler(this.DataGridView_SelectionChanged);
DataGridViewSelectionChangedIsOn = false;
}
DataGridViewComboEditBoxCell comboEditBoxCell =
this.dataGridView.CurrentCell as DataGridViewComboEditBoxCell;
if (comboEditBoxCell != null)
{
if (comboEditBoxCell.GetComboBoxValidatingIsOn())
{
ComboBox comboBox = this.dataGridView.EditingControl as ComboBox;
if (comboBox != null)
{
comboBox.Validating -=
new CancelEventHandler(comboEditBoxCell.comboBox_Validating);
}
}
}
StopProcessingDataErrorIsOn = true;
}
Points of Interest
There are many methods to solve the event handling problems that we experience. In this article, I'm expecting my example will help you find out your solutions.