Click here to Skip to main content
Licence CPOL
First Posted 15 May 2007
Views 15,916
Downloads 182
Bookmarked 13 times

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

By | 15 May 2007 | Article
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.

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:

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:

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)

About the Author

azealer

Web Developer

Japan Japan

Member

Ph.D(Computer Network)
Project Manger
Developer

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUseful when programming windows forms! PinmemberWebdiyer22:22 15 May '07  
GeneralWow.. this is great stuff.. [modified] Pinmembersnurfkin19:18 15 May '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 15 May 2007
Article Copyright 2007 by azealer
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid