Dynamically adding a action to a datagrid
-
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();
-
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();
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
-
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();
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.”
-
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.”
Do you need to dispose of the event explicitly?
Never underestimate the power of human stupidity RAH
-
Do you need to dispose of the event explicitly?
Never underestimate the power of human stupidity RAH
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.”
-
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.”
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
-
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
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.”
-
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.”
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));
-
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));
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 thedataGridTicketInformation_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 goDataGridView 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 ofdataGridTicketInformation_DoubleClick
, it is important that you use theDataGridView 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)
-
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 thedataGridTicketInformation_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 goDataGridView 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 ofdataGridTicketInformation_DoubleClick
, it is important that you use theDataGridView 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)
-
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);
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.”