Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

A method that provides how to handle events when maintaining multiple tables on a DataGridView

1.31/5 (3 votes)
15 May 2007CPOL1 min read 1   367  
This article shows a method which handles events when you maintain multiple tables on a DataGridView.

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.

C#
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;
}

I inserted stop-processing into the DataError handler as follows:

C#
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;
     }        
}

You can unload the event handlers using the flag as follows:

C#
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;
}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)