Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. C# Datagridview

C# Datagridview

Scheduled Pinned Locked Moved C#
databasecsharphelpquestionannouncement
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ronni Marker
    wrote on last edited by
    #1

    Hi, I have a datagridview with a combobox. to find out when the combobox have changed values i included the following code

        private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            ComboBox cbo = e.Control as ComboBox;
            if (cbo != null)
            {
                cbo.SelectionChangeCommitted += new EventHandler(cbo\_SelectionChangeCommitted);
            }
        }
    
        private void cbo\_SelectionChangeCommitted(object sender, EventArgs e)
        {
                MessageBox.Show(((ComboBox)sender).Text.ToString());
        }
    

    But the problem for me is that I realised that the messagebox gets triggered once for the first time i change a value in a row. But the second time i change value it gets triggered twice, third time it gets triggered trice and so furth. So, I was wondering if there is any way to only trigger the SelectionChangeCommitted once every time a user changes the combobox value? It will be a rather large number of rows, and if a user updates 200 rows, then it will mean that SelectionChangeCommitted will be triggered 200 times at the end and I can't have that as the messagebox should be replaced with a webservice that will update a value in a database at runtime. Any suggestion would be appreciated.

    D R 2 Replies Last reply
    0
    • R Ronni Marker

      Hi, I have a datagridview with a combobox. to find out when the combobox have changed values i included the following code

          private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
          {
              ComboBox cbo = e.Control as ComboBox;
              if (cbo != null)
              {
                  cbo.SelectionChangeCommitted += new EventHandler(cbo\_SelectionChangeCommitted);
              }
          }
      
          private void cbo\_SelectionChangeCommitted(object sender, EventArgs e)
          {
                  MessageBox.Show(((ComboBox)sender).Text.ToString());
          }
      

      But the problem for me is that I realised that the messagebox gets triggered once for the first time i change a value in a row. But the second time i change value it gets triggered twice, third time it gets triggered trice and so furth. So, I was wondering if there is any way to only trigger the SelectionChangeCommitted once every time a user changes the combobox value? It will be a rather large number of rows, and if a user updates 200 rows, then it will mean that SelectionChangeCommitted will be triggered 200 times at the end and I can't have that as the messagebox should be replaced with a webservice that will update a value in a database at runtime. Any suggestion would be appreciated.

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      Everytime the dataGridView1_EditingControlShowing method gets called you are adding a new entry to the delegate's (cbo.SelectionChangeCommitted) invocation list with ... **+=** ... You should either move the event subscription to somewhere else that only gets called once, unsubscribe using ... **-=** ..., or keep a bool flag (array or list of bools if more than one control) and check it before the subscription - if false, set to true and subscribe; if true skip subscription.

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      R 1 Reply Last reply
      0
      • R Ronni Marker

        Hi, I have a datagridview with a combobox. to find out when the combobox have changed values i included the following code

            private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                ComboBox cbo = e.Control as ComboBox;
                if (cbo != null)
                {
                    cbo.SelectionChangeCommitted += new EventHandler(cbo\_SelectionChangeCommitted);
                }
            }
        
            private void cbo\_SelectionChangeCommitted(object sender, EventArgs e)
            {
                    MessageBox.Show(((ComboBox)sender).Text.ToString());
            }
        

        But the problem for me is that I realised that the messagebox gets triggered once for the first time i change a value in a row. But the second time i change value it gets triggered twice, third time it gets triggered trice and so furth. So, I was wondering if there is any way to only trigger the SelectionChangeCommitted once every time a user changes the combobox value? It will be a rather large number of rows, and if a user updates 200 rows, then it will mean that SelectionChangeCommitted will be triggered 200 times at the end and I can't have that as the messagebox should be replaced with a webservice that will update a value in a database at runtime. Any suggestion would be appreciated.

        R Offline
        R Offline
        ricmil42
        wrote on last edited by
        #3

        It looks like each time you show the editor you are running this line:

        cbo.SelectionChangeCommitted += new EventHandler(cbo_SelectionChangeCommitted);

        Since += is used the events are additive. I assume the grid has an event for the editor closing? Sorry I use DevX controls and don't really know the MS grid. Find the editor close event and run this line:

        cbo.SelectionChangeCommitted -= new EventHandler(cbo_SelectionChangeCommitted);

        This will remove the event that is showing the message box. Opening the editor attaches the message box event, closing the editor releases it.

        1 Reply Last reply
        0
        • D DaveyM69

          Everytime the dataGridView1_EditingControlShowing method gets called you are adding a new entry to the delegate's (cbo.SelectionChangeCommitted) invocation list with ... **+=** ... You should either move the event subscription to somewhere else that only gets called once, unsubscribe using ... **-=** ..., or keep a bool flag (array or list of bools if more than one control) and check it before the subscription - if false, set to true and subscribe; if true skip subscription.

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          R Offline
          R Offline
          Ronni Marker
          wrote on last edited by
          #4

          Man.... Your right. Thanks, had been staring myself blind at what I already knew. Cheers, Ronni

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups