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
  1. Home
  2. General Programming
  3. C#
  4. DataGridView SelectionChanged event

DataGridView SelectionChanged event

Scheduled Pinned Locked Moved C#
question
4 Posts 2 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.
  • E Offline
    E Offline
    eyalbi007
    wrote on last edited by
    #1

    Hi, A quote from MSDN about this event: "This event occurs whenever cells are selected or the selection is canceled, whether programmatically or by user action". My question: How can I get the exact same functionality, but only as a result of user action (I do not wish this even to occur when the selection has programmatically changed). Thanks, Eyal.

    D 1 Reply Last reply
    0
    • E eyalbi007

      Hi, A quote from MSDN about this event: "This event occurs whenever cells are selected or the selection is canceled, whether programmatically or by user action". My question: How can I get the exact same functionality, but only as a result of user action (I do not wish this even to occur when the selection has programmatically changed). Thanks, Eyal.

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      What I can think of is to have a flag (boolean variable) in your form. Now, when you are changing selected cell through code, set this to false and make it true again in the end of event. Something like this:

      bool isChangedByUser = true; // class level variable
      .
      .
      .
      isChangedByUser = false;
      // Some code block that changes selected cell
      .
      .
      .
      void dgv_SelectionChanged(object sender, EventArgs e)
      {
      if(isChangedByUser)
      {
      // Do something
      }
      isChangedByUser = true;
      }

      E 1 Reply Last reply
      0
      • D dan sh

        What I can think of is to have a flag (boolean variable) in your form. Now, when you are changing selected cell through code, set this to false and make it true again in the end of event. Something like this:

        bool isChangedByUser = true; // class level variable
        .
        .
        .
        isChangedByUser = false;
        // Some code block that changes selected cell
        .
        .
        .
        void dgv_SelectionChanged(object sender, EventArgs e)
        {
        if(isChangedByUser)
        {
        // Do something
        }
        isChangedByUser = true;
        }

        E Offline
        E Offline
        eyalbi007
        wrote on last edited by
        #3

        Thank for the reply. The problem is not that I'm changing the selection - in this case the problem is easy to solve. The problem is that the event is fired when the form is initially opened and rows are automatically added. Then, when the first row is (automatically) selected, the event is fired. Eyal.

        D 1 Reply Last reply
        0
        • E eyalbi007

          Thank for the reply. The problem is not that I'm changing the selection - in this case the problem is easy to solve. The problem is that the event is fired when the form is initially opened and rows are automatically added. Then, when the first row is (automatically) selected, the event is fired. Eyal.

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          Following are two of the various ways you can do this: 1. Attach the event handler once you are done with filling the DataGridView with data. 2. Have a boolean variable and use it to decide if the code inside the event handler should be executed or not.

          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