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. Dynamically adding a action to a datagrid

Dynamically adding a action to a datagrid

Scheduled Pinned Locked Moved C#
graphicstutorialquestion
11 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.
  • B Offline
    B Offline
    Brad Wick
    wrote on last edited by
    #1

    In this application I am playing with, I am generating a tabcontrol on the fly where each tab is a folder name in a directory. I am then dynamically adding a new DataGridView onto the new tab that was created and listing out the files inside of this directory (there are no sub directorys which is the reason I am doing it this way). This is all working file exactly like what I want except I can not figure out how to build a dynamic onrow click event because I have no idea how many tabs the application will have in the future. Can someone point me in the correct direction? Listed below is the code that is looped through when adding the dataGrid to each tab (notice the name has the tabIndex). DataGridView dataGridTicketInformation = new System.Windows.Forms.DataGridView(); dataGridTicketInformation.Name = "dataGridView" + tabIndex.ToString(); dataGridTicketInformation.Size = new System.Drawing.Size(401, 201); // First we need to add a column dataGridTicketInformation.Columns.Clear(); dataGridTicketInformation.Columns.Add("Name", "Name"); dataGridTicketInformation.Columns[0].Width = 301; dataGridTicketInformation.Columns.Add("Data", "Data"); dataGridTicketInformation.Columns[1].Width = 100; // Clear all the old Data first dataGridTicketInformation.Rows.Clear();

    M H 2 Replies Last reply
    0
    • B Brad Wick

      In this application I am playing with, I am generating a tabcontrol on the fly where each tab is a folder name in a directory. I am then dynamically adding a new DataGridView onto the new tab that was created and listing out the files inside of this directory (there are no sub directorys which is the reason I am doing it this way). This is all working file exactly like what I want except I can not figure out how to build a dynamic onrow click event because I have no idea how many tabs the application will have in the future. Can someone point me in the correct direction? Listed below is the code that is looped through when adding the dataGrid to each tab (notice the name has the tabIndex). DataGridView dataGridTicketInformation = new System.Windows.Forms.DataGridView(); dataGridTicketInformation.Name = "dataGridView" + tabIndex.ToString(); dataGridTicketInformation.Size = new System.Drawing.Size(401, 201); // First we need to add a column dataGridTicketInformation.Columns.Clear(); dataGridTicketInformation.Columns.Add("Name", "Name"); dataGridTicketInformation.Columns[0].Width = 301; dataGridTicketInformation.Columns.Add("Data", "Data"); dataGridTicketInformation.Columns[1].Width = 100; // Clear all the old Data first dataGridTicketInformation.Rows.Clear();

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      I'm assuming you know how to add an event to the datagridviw this.dataGridTicketInformation.DoubleClick += new System.EventHandler(this.GenericGridEvent); GenericGridEvent would deal with the DGV getting the identity from the sender object There is no RowClick event on the DGV, pick one of the other events to add.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • B Brad Wick

        In this application I am playing with, I am generating a tabcontrol on the fly where each tab is a folder name in a directory. I am then dynamically adding a new DataGridView onto the new tab that was created and listing out the files inside of this directory (there are no sub directorys which is the reason I am doing it this way). This is all working file exactly like what I want except I can not figure out how to build a dynamic onrow click event because I have no idea how many tabs the application will have in the future. Can someone point me in the correct direction? Listed below is the code that is looped through when adding the dataGrid to each tab (notice the name has the tabIndex). DataGridView dataGridTicketInformation = new System.Windows.Forms.DataGridView(); dataGridTicketInformation.Name = "dataGridView" + tabIndex.ToString(); dataGridTicketInformation.Size = new System.Drawing.Size(401, 201); // First we need to add a column dataGridTicketInformation.Columns.Clear(); dataGridTicketInformation.Columns.Add("Name", "Name"); dataGridTicketInformation.Columns[0].Width = 301; dataGridTicketInformation.Columns.Add("Data", "Data"); dataGridTicketInformation.Columns[1].Width = 100; // Clear all the old Data first dataGridTicketInformation.Rows.Clear();

        H Offline
        H Offline
        Henry Minute
        wrote on last edited by
        #3

        If I have correctly understood what it is that you want to do. Create a RowHeaderMouseClick event handler like this (I assume that this is the handler you want)

        	private void DataGridView\_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        	{
                        DataGridView dgv = (DataGridView(sender;
        
                        // do whatever you want here dgv points to the grid that had its row clicked
                        dgv.CurrentRow.Cells\["Name"\].Value = "Fred"; // or whatever
        	}
        

        The same principle applies to any handler for multiple controls. Then in the code that you posted, I dont think it matters where as long as it is after instantiation:

        dataGridTicketInformation.RowHeaderClick += DataGridView\_RowHeaderMouseClick;
        

        BTW if the grid is disposed befor closing the app by using:

        dataGridTicketInformation.RowHeaderClick -= DataGridView\_RowHeaderMouseClick;
        

        Hope this helps.

        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

        M B 2 Replies Last reply
        0
        • H Henry Minute

          If I have correctly understood what it is that you want to do. Create a RowHeaderMouseClick event handler like this (I assume that this is the handler you want)

          	private void DataGridView\_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
          	{
                          DataGridView dgv = (DataGridView(sender;
          
                          // do whatever you want here dgv points to the grid that had its row clicked
                          dgv.CurrentRow.Cells\["Name"\].Value = "Fred"; // or whatever
          	}
          

          The same principle applies to any handler for multiple controls. Then in the code that you posted, I dont think it matters where as long as it is after instantiation:

          dataGridTicketInformation.RowHeaderClick += DataGridView\_RowHeaderMouseClick;
          

          BTW if the grid is disposed befor closing the app by using:

          dataGridTicketInformation.RowHeaderClick -= DataGridView\_RowHeaderMouseClick;
          

          Hope this helps.

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Do you need to dispose of the event explicitly?

          Never underestimate the power of human stupidity RAH

          H 1 Reply Last reply
          0
          • M Mycroft Holmes

            Do you need to dispose of the event explicitly?

            Never underestimate the power of human stupidity RAH

            H Offline
            H Offline
            Henry Minute
            wrote on last edited by
            #5

            I have had problems in the past that although I never managed to track down the exact cause, disappeared after doing so, so I always do now. I think it is recommended somewhere in MSDN too.

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            M 1 Reply Last reply
            0
            • H Henry Minute

              I have had problems in the past that although I never managed to track down the exact cause, disappeared after doing so, so I always do now. I think it is recommended somewhere in MSDN too.

              Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              I am starting to use the 1 event for lots of controls, in VB you could just add the control to the Handle list, for things like turning the save button on after a value is edited, the event does not care about the sender just turns on the save button. I presume I should dispose each event on each control when leaving the form!

              Never underestimate the power of human stupidity RAH

              H 1 Reply Last reply
              0
              • M Mycroft Holmes

                I am starting to use the 1 event for lots of controls, in VB you could just add the control to the Handle list, for things like turning the save button on after a value is edited, the event does not care about the sender just turns on the save button. I presume I should dispose each event on each control when leaving the form!

                Never underestimate the power of human stupidity RAH

                H Offline
                H Offline
                Henry Minute
                wrote on last edited by
                #7

                Mycroft Holmes wrote:

                I presume I should dispose each event on each control when leaving the form!

                If the Form will be Closed (i.e. Disposed) then it is not necessary. Where I have used this method is for dynamically created controls, such as described by the OP. Where these controls are disposed but their parent control/form is still live, I detach the event handler. I think the reasoning is that there will still be a reference to the disposed control, although convoluted, via the events mechanism. This will prevent it being Garbage Collected.

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                1 Reply Last reply
                0
                • H Henry Minute

                  If I have correctly understood what it is that you want to do. Create a RowHeaderMouseClick event handler like this (I assume that this is the handler you want)

                  	private void DataGridView\_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
                  	{
                                  DataGridView dgv = (DataGridView(sender;
                  
                                  // do whatever you want here dgv points to the grid that had its row clicked
                                  dgv.CurrentRow.Cells\["Name"\].Value = "Fred"; // or whatever
                  	}
                  

                  The same principle applies to any handler for multiple controls. Then in the code that you posted, I dont think it matters where as long as it is after instantiation:

                  dataGridTicketInformation.RowHeaderClick += DataGridView\_RowHeaderMouseClick;
                  

                  BTW if the grid is disposed befor closing the app by using:

                  dataGridTicketInformation.RowHeaderClick -= DataGridView\_RowHeaderMouseClick;
                  

                  Hope this helps.

                  Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                  B Offline
                  B Offline
                  Brad Wick
                  wrote on last edited by
                  #8

                  Im just not able to get this going, so hopefully you can help. The error I am getting is Method Name expected on "dataGridTicketInformation, new CustomRowEventArgs(dataGridTicketInformation" I first specify the CustomRowEventArgs and the DoubleClick function

                  public class CustomRowEventArgs : EventArgs
                  {
                  DataGridView grid;
                  public DataGridView Grid
                  {
                  get { return grid; }
                  }
                  public CustomRowEventArgs(DataGridView dgv)
                  {
                  grid = dgv;

                  }
                  

                  }

                  private void dataGridTicketInformation_DoubleClick(object sender, CustomRowEventArgs e)
                  {
                  MessageBox.Show("DoubleClick Sent");
                  }

                  Then after I dynamically build and load all the information into the datagrid I call the doubleclick function

                  dataGridTicketInformation.DoubleClick += new System.EventHandler(dataGridTicketInformation, new CustomRowEventArgs(dataGridTicketInformation));

                  H 1 Reply Last reply
                  0
                  • B Brad Wick

                    Im just not able to get this going, so hopefully you can help. The error I am getting is Method Name expected on "dataGridTicketInformation, new CustomRowEventArgs(dataGridTicketInformation" I first specify the CustomRowEventArgs and the DoubleClick function

                    public class CustomRowEventArgs : EventArgs
                    {
                    DataGridView grid;
                    public DataGridView Grid
                    {
                    get { return grid; }
                    }
                    public CustomRowEventArgs(DataGridView dgv)
                    {
                    grid = dgv;

                    }
                    

                    }

                    private void dataGridTicketInformation_DoubleClick(object sender, CustomRowEventArgs e)
                    {
                    MessageBox.Show("DoubleClick Sent");
                    }

                    Then after I dynamically build and load all the information into the datagrid I call the doubleclick function

                    dataGridTicketInformation.DoubleClick += new System.EventHandler(dataGridTicketInformation, new CustomRowEventArgs(dataGridTicketInformation));

                    H Offline
                    H Offline
                    Henry Minute
                    wrote on last edited by
                    #9

                    OK. This line:

                    dataGridTicketInformation.DoubleClick += new System.EventHandler(dataGridTicketInformation, new CustomRowEventArgs(dataGridTicketInformation));

                    should be:

                    dataGridTicketInformation.DoubleClick += new System.EventHandler(dataGridTicketInformation);

                    ***** See below for where to put it

                    and it is not used to call the doubleclick function it is used to link each DataGridView with the dataGridTicketInformation_DoubleClick method you have written, so that when you doubleclick on any of your grids you will get your "DoubleClick Sent" messageBox. It should be part of the creation of each DataGridView the line should go

                    DataGridView dataGridTicketInformation = new System.Windows.Forms.DataGridView();
                    dataGridTicketInformation.Name = "dataGridView" + tabIndex.ToString();
                    dataGridTicketInformation.Size = new System.Drawing.Size(401, 201);

                    // First we need to add a column
                    dataGridTicketInformation.Columns.Clear();
                    dataGridTicketInformation.Columns.Add("Name", "Name");
                    dataGridTicketInformation.Columns[0].Width = 301;

                    dataGridTicketInformation.Columns.Add("Data", "Data");
                    dataGridTicketInformation.Columns[1].Width = 100;

                    // Clear all the old Data first
                    dataGridTicketInformation.Rows.Clear();

                    // ***************************************
                    // HERE (ish)
                    // ***************************************
                    dataGridTicketInformation.DoubleClick += new System.EventHandler(dataGridTicketInformation);

                    Unless there is a need to pass some special data on double click the CustomRowEventArgs is not needed. Even if this need exists, I would concentrate on getting the plain old doubleclick functionality working first, so that you can get an understanding of how it works. Make a separate post if you need help in implementing a CustomRowEventArgs. Assuming that you do this, all that needs to happen for the doubleclick action to be triggered is for you to double-click any of the dataGridTicketInformation instances that your app creates at run-time. When you get it working with your messageBox version of dataGridTicketInformation_DoubleClick, it is important that you use the

                    DataGridView dgv = (DataGridView)sender;
                    

                    method I showed earlier, otherwise you will not be able to tell which dataGridTicketInformation made the call.

                    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring)

                    B 1 Reply Last reply
                    0
                    • H Henry Minute

                      OK. This line:

                      dataGridTicketInformation.DoubleClick += new System.EventHandler(dataGridTicketInformation, new CustomRowEventArgs(dataGridTicketInformation));

                      should be:

                      dataGridTicketInformation.DoubleClick += new System.EventHandler(dataGridTicketInformation);

                      ***** See below for where to put it

                      and it is not used to call the doubleclick function it is used to link each DataGridView with the dataGridTicketInformation_DoubleClick method you have written, so that when you doubleclick on any of your grids you will get your "DoubleClick Sent" messageBox. It should be part of the creation of each DataGridView the line should go

                      DataGridView dataGridTicketInformation = new System.Windows.Forms.DataGridView();
                      dataGridTicketInformation.Name = "dataGridView" + tabIndex.ToString();
                      dataGridTicketInformation.Size = new System.Drawing.Size(401, 201);

                      // First we need to add a column
                      dataGridTicketInformation.Columns.Clear();
                      dataGridTicketInformation.Columns.Add("Name", "Name");
                      dataGridTicketInformation.Columns[0].Width = 301;

                      dataGridTicketInformation.Columns.Add("Data", "Data");
                      dataGridTicketInformation.Columns[1].Width = 100;

                      // Clear all the old Data first
                      dataGridTicketInformation.Rows.Clear();

                      // ***************************************
                      // HERE (ish)
                      // ***************************************
                      dataGridTicketInformation.DoubleClick += new System.EventHandler(dataGridTicketInformation);

                      Unless there is a need to pass some special data on double click the CustomRowEventArgs is not needed. Even if this need exists, I would concentrate on getting the plain old doubleclick functionality working first, so that you can get an understanding of how it works. Make a separate post if you need help in implementing a CustomRowEventArgs. Assuming that you do this, all that needs to happen for the doubleclick action to be triggered is for you to double-click any of the dataGridTicketInformation instances that your app creates at run-time. When you get it working with your messageBox version of dataGridTicketInformation_DoubleClick, it is important that you use the

                      DataGridView dgv = (DataGridView)sender;
                      

                      method I showed earlier, otherwise you will not be able to tell which dataGridTicketInformation made the call.

                      Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring)

                      B Offline
                      B Offline
                      Brad Wick
                      wrote on last edited by
                      #10

                      If I add that code I get a error on the EventHandler ('dataGridTicketInformation' is a 'variable' but is used like a 'method'). Any ideas? dataGridTicketInformation.DoubleClick += new System.EventHandler(dataGridTicketInformation);

                      H 1 Reply Last reply
                      0
                      • B Brad Wick

                        If I add that code I get a error on the EventHandler ('dataGridTicketInformation' is a 'variable' but is used like a 'method'). Any ideas? dataGridTicketInformation.DoubleClick += new System.EventHandler(dataGridTicketInformation);

                        H Offline
                        H Offline
                        Henry Minute
                        wrote on last edited by
                        #11

                        Sorry! My bad, over-eager with the cut and paste. :-O Should be

                        dataGridTicketInformation.DoubleClick += new System.EventHandler(dataGridTicketInformation_DoubleClick);

                        Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                        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