ComboBox selection generating list for another comboBox question
-
Hi all, I have the following code I'm trying to get working. When I change the selection in the comboBox in column1, I want the list generated if the selection was "Test1". However, it seems no list is generated. Any help would be appreciated. Thanks.
public class Units
{
public string Unit { get; set; }} private void column1DataGridViewTextBoxColumn\_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e) { string strPrimary = column1DataGridViewTextBoxColumn.ToString(); Debug.WriteLine(strPrimary); if (strPrimary == "Test1") { //Build a list var dataSource = new List<Units>(); dataSource.Add(new Units() { Unit = "blah1" }); dataSource.Add(new Units() { Unit = "blah2" }); dataSource.Add(new Units() { Unit = "blah3" }); //Setup data binding this.column2DataGridViewTextBoxColumn.DataSource = dataSource; this.column2DataGridViewTextBoxColumn.DisplayMember = "Unit"; } }
-
Hi all, I have the following code I'm trying to get working. When I change the selection in the comboBox in column1, I want the list generated if the selection was "Test1". However, it seems no list is generated. Any help would be appreciated. Thanks.
public class Units
{
public string Unit { get; set; }} private void column1DataGridViewTextBoxColumn\_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e) { string strPrimary = column1DataGridViewTextBoxColumn.ToString(); Debug.WriteLine(strPrimary); if (strPrimary == "Test1") { //Build a list var dataSource = new List<Units>(); dataSource.Add(new Units() { Unit = "blah1" }); dataSource.Add(new Units() { Unit = "blah2" }); dataSource.Add(new Units() { Unit = "blah3" }); //Setup data binding this.column2DataGridViewTextBoxColumn.DataSource = dataSource; this.column2DataGridViewTextBoxColumn.DisplayMember = "Unit"; } }
Wow...I'm really confused by your code. Is column1 a ComboBox column or a TextBox column? Because you named it
column1DataGridView**TextBox**Column
. But let's say that it actually is a ComboBox column. What is it that you thinkcolumn1DataGridViewTextBoxColumn.ToString()
does? It creates a string that gives you basic information about the object. So, for instance, I added a ComboBox column to a DataGridView. TheToString()
method returns "DataGridViewComboBoxColumn { Name=Column1, Index=0 }". That's what you're checking to see if it equals "Test1". It never will. What you want to be checking is:string strPrimary = (ComboBox)sender.SelectedText;
I'd say if you don't have this kind of basic understanding as to what these methods do, you really shouldn't be trying to mess with DataGridView's and Lists. Seriously, start out with a basic course on C#.
-
Hi all, I have the following code I'm trying to get working. When I change the selection in the comboBox in column1, I want the list generated if the selection was "Test1". However, it seems no list is generated. Any help would be appreciated. Thanks.
public class Units
{
public string Unit { get; set; }} private void column1DataGridViewTextBoxColumn\_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e) { string strPrimary = column1DataGridViewTextBoxColumn.ToString(); Debug.WriteLine(strPrimary); if (strPrimary == "Test1") { //Build a list var dataSource = new List<Units>(); dataSource.Add(new Units() { Unit = "blah1" }); dataSource.Add(new Units() { Unit = "blah2" }); dataSource.Add(new Units() { Unit = "blah3" }); //Setup data binding this.column2DataGridViewTextBoxColumn.DataSource = dataSource; this.column2DataGridViewTextBoxColumn.DisplayMember = "Unit"; } }
Try replacing this line
string strPrimary = column1DataGridViewTextBoxColumn.ToString();
with
string strPrimary = dataGridView1\[e.ColumnIndex, e.RowIndex\].Value.ToString();
What you were getting before was something like "DataGridViewComboBoxColumn{something="ibbledibble"}", rather than "Test1". In other words the DataGridViewComboBoxColumn class
ToString
, rather than the contentToString
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.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.
-
Hi all, I have the following code I'm trying to get working. When I change the selection in the comboBox in column1, I want the list generated if the selection was "Test1". However, it seems no list is generated. Any help would be appreciated. Thanks.
public class Units
{
public string Unit { get; set; }} private void column1DataGridViewTextBoxColumn\_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e) { string strPrimary = column1DataGridViewTextBoxColumn.ToString(); Debug.WriteLine(strPrimary); if (strPrimary == "Test1") { //Build a list var dataSource = new List<Units>(); dataSource.Add(new Units() { Unit = "blah1" }); dataSource.Add(new Units() { Unit = "blah2" }); dataSource.Add(new Units() { Unit = "blah3" }); //Setup data binding this.column2DataGridViewTextBoxColumn.DataSource = dataSource; this.column2DataGridViewTextBoxColumn.DisplayMember = "Unit"; } }
To add to this, you shouldn't have known that
column1DataGridViewTextBoxColumn.ToString()
wasn't giving you the right result because right after you made that called, you printed the result to the debugger. That didn't tell you anything? You couldn't tell right away that you weren't getting what you expected and that that was the problem? -
To add to this, you shouldn't have known that
column1DataGridViewTextBoxColumn.ToString()
wasn't giving you the right result because right after you made that called, you printed the result to the debugger. That didn't tell you anything? You couldn't tell right away that you weren't getting what you expected and that that was the problem?William, "Wow...I'm really confused by your code. Is column1 a ComboBox column or a TextBox column? Because you named it column1DataGridViewTextBoxColumn" As I'm just trying to get through the mechanics of this, I wasn't worried about changing the names, but rather leaving them as default (as I changed the column type to a comboBox through the IDE). That is noted for the next time I post something.
William Winner wrote:
That didn't tell you anything? You couldn't tell right away that you weren't getting what you expected and that that was the problem?
Yes. It told me that I wasn't doing something correctly. That is why I posted to get some help (sometimes kind and sometimes.....) on this forum. I didn't know where the problem was. I have made it through James Foxall's C# in 24 hours and also tons of internet tutorials. As you noted, I am not an expert and I am trying to do something way to complex for me and unfortunately I get stuck on the simple things. Unfortunately, I cannot afford a C# programmer for this project and have spent many hours away from family. Truth be told, I am a hands-on mechanical engineer trying to write software, which has proven to be painful, but also gratifying when things actually work. Not looking for sympathy here, but some courtesy would be appreciated. However, I do appreciate your suggestions, even though the wrapping wasn't too tasty.
-
Try replacing this line
string strPrimary = column1DataGridViewTextBoxColumn.ToString();
with
string strPrimary = dataGridView1\[e.ColumnIndex, e.RowIndex\].Value.ToString();
What you were getting before was something like "DataGridViewComboBoxColumn{something="ibbledibble"}", rather than "Test1". In other words the DataGridViewComboBoxColumn class
ToString
, rather than the contentToString
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.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.
Henry Minute wrote:
What you were getting before was something like "DataGridViewComboBoxColumn{something="ibbledibble"}", rather than "Test1". In other words the DataGridViewComboBoxColumn class ToString, rather than the content ToString
I was getting that if I removed the following lines and put them under the form load method.
string strPrimary = column1DataGridViewTextBoxColumn.DisplayIndex.ToString();
Debug.WriteLine(strPrimary);If I left it under
public void column1DataGridViewTextBoxColumn_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e)
it didn't execute that when I changed the comboBox value. With that said, I understand your suggestion, but the SelectedIndexChanged method isn't being triggered for some reason.
-
Henry Minute wrote:
What you were getting before was something like "DataGridViewComboBoxColumn{something="ibbledibble"}", rather than "Test1". In other words the DataGridViewComboBoxColumn class ToString, rather than the content ToString
I was getting that if I removed the following lines and put them under the form load method.
string strPrimary = column1DataGridViewTextBoxColumn.DisplayIndex.ToString();
Debug.WriteLine(strPrimary);If I left it under
public void column1DataGridViewTextBoxColumn_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e)
it didn't execute that when I changed the comboBox value. With that said, I understand your suggestion, but the SelectedIndexChanged method isn't being triggered for some reason.
This is how to get the SelectedIndexChanged event handler hooked up properly. Handle the
EditingControlShowing
event of yourDataGridView
. It's rather nifty.private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox cb = e.Control as ComboBox; if (cb != null) { // first remove event handler to keep from attaching multiple: cb.SelectedIndexChanged -= cb\_SelectedIndexChanged; // now attach the event handler cb.SelectedIndexChanged += cb\_SelectedIndexChanged; } } void cb\_SelectedIndexChanged(object sender, EventArgs e) { MessageBox.Show("Selected index changed"); }
Simply substitute the correct control names/indexes to suit your application. Good luck! :) [Edit] You can refine this slightly to enable handling SelectedIndexChanged events for more than one ComboBoxColumn.
private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox cb; switch (dataGrid.CurrentCell.ColumnIndex) { case 0: // use the index for your comboboxcolumn cb = e.Control as ComboBox; if (cb != null) { // first remove event handler to keep from attaching multiple: cb.SelectedIndexChanged -= cb\_SelectedIndexChanged; // now attach the event handler cb.SelectedIndexChanged += cb\_SelectedIndexChanged; } break; case 3: // use the index for your second combo cb = e.Control as ComboBox; if (cb != null) { // first remove event handler to keep from attaching multiple: cb.SelectedIndexChanged -= cb\_SecondSelectedIndexChanged; // now attach the event handler cb.SelectedIndexChanged += cb\_SecondSelectedIndexChanged; } break; }
[/Edit] 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.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.
modified on Tuesday, May 18, 2010 2:53 PM
-
This is how to get the SelectedIndexChanged event handler hooked up properly. Handle the
EditingControlShowing
event of yourDataGridView
. It's rather nifty.private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox cb = e.Control as ComboBox; if (cb != null) { // first remove event handler to keep from attaching multiple: cb.SelectedIndexChanged -= cb\_SelectedIndexChanged; // now attach the event handler cb.SelectedIndexChanged += cb\_SelectedIndexChanged; } } void cb\_SelectedIndexChanged(object sender, EventArgs e) { MessageBox.Show("Selected index changed"); }
Simply substitute the correct control names/indexes to suit your application. Good luck! :) [Edit] You can refine this slightly to enable handling SelectedIndexChanged events for more than one ComboBoxColumn.
private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox cb; switch (dataGrid.CurrentCell.ColumnIndex) { case 0: // use the index for your comboboxcolumn cb = e.Control as ComboBox; if (cb != null) { // first remove event handler to keep from attaching multiple: cb.SelectedIndexChanged -= cb\_SelectedIndexChanged; // now attach the event handler cb.SelectedIndexChanged += cb\_SelectedIndexChanged; } break; case 3: // use the index for your second combo cb = e.Control as ComboBox; if (cb != null) { // first remove event handler to keep from attaching multiple: cb.SelectedIndexChanged -= cb\_SecondSelectedIndexChanged; // now attach the event handler cb.SelectedIndexChanged += cb\_SecondSelectedIndexChanged; } break; }
[/Edit] 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.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.
modified on Tuesday, May 18, 2010 2:53 PM
Check out my picture on my profile. There is a reason I put that picture on there. That is about how I feel at the moment (and others) :| It seems you are instantiating ComboBox cb. I am confused on a couple of things here. If I have already changed a column type to a comboBox via the IDE, how does it tie in here? Should I be using the DataPropertyName under "Data" in the IDE or (Name) under "Design" in the IDE when referring. I have a generic question about binding, but I'll post a new message for that.
-
William, "Wow...I'm really confused by your code. Is column1 a ComboBox column or a TextBox column? Because you named it column1DataGridViewTextBoxColumn" As I'm just trying to get through the mechanics of this, I wasn't worried about changing the names, but rather leaving them as default (as I changed the column type to a comboBox through the IDE). That is noted for the next time I post something.
William Winner wrote:
That didn't tell you anything? You couldn't tell right away that you weren't getting what you expected and that that was the problem?
Yes. It told me that I wasn't doing something correctly. That is why I posted to get some help (sometimes kind and sometimes.....) on this forum. I didn't know where the problem was. I have made it through James Foxall's C# in 24 hours and also tons of internet tutorials. As you noted, I am not an expert and I am trying to do something way to complex for me and unfortunately I get stuck on the simple things. Unfortunately, I cannot afford a C# programmer for this project and have spent many hours away from family. Truth be told, I am a hands-on mechanical engineer trying to write software, which has proven to be painful, but also gratifying when things actually work. Not looking for sympathy here, but some courtesy would be appreciated. However, I do appreciate your suggestions, even though the wrapping wasn't too tasty.
It would have solved some issues if we knew that you were never getting to the SelectedIndexChanged code. I assumed that you had already handle the EditControlShowing event and added the event handler for the SelectedIndexChanged event. Henry gave you code to do this, but I can try to explain what is happening since you seemed a bit confused about it. When you create the DataGridView, you can specify columns and specify which column within a table or a datasource that that column is linked to. You can also set up what type of control the column is edited with. You can choose a simple TextBox, or you can have other types, for instance the ComboBox type. When the user clicks within a cell, the DataGridView then checks that column to see what type of control to use to allow the user to edit the cell. This is called the EditControl. If the column type was selected to be a ComboBox, then it will use a ComboBox that the DataGridView created to allow the user to edit the value. It creates the control once and then re-uses it. The EditControlShowing event is set off when the control is actually shown. This ComboBox is just a standard ComboBox and has all of the same events and properties as a standard ComboBox. One of the arguments passed during this event is e.Control. This is the control that is being used. It won't always be a ComboBox...it will be whatever type of control that is being used for that column. So, when that control is passed, you then need to add a handler to whichever event you are interested in...in this case, the SelectedIndexChanged event. You also want to make sure to remove the handler afterwards so that it doesn't get called more than once. In Henry's case, he removed it before adding a new one which is perfectly reasonable since the user could change the selection more than once without the EditControl changing. This may be a bit rambly, but I hope it helps with your understanding of what's happening. I wish you luck with this, but I'm still going to say that an internet course without feedback and online tutorials isn't going to get you far. Your company may not be able to afford a programmer, and it may not fit the timetable for this particular project, but I would suggest finding an entry level course on programming at your community college or state university. You can generally do that pretty cheaply and having an instructor that you can ask questions of can help a ton.
-
Check out my picture on my profile. There is a reason I put that picture on there. That is about how I feel at the moment (and others) :| It seems you are instantiating ComboBox cb. I am confused on a couple of things here. If I have already changed a column type to a comboBox via the IDE, how does it tie in here? Should I be using the DataPropertyName under "Data" in the IDE or (Name) under "Design" in the IDE when referring. I have a generic question about binding, but I'll post a new message for that.
OK I have looked at your picture and I have to say that I am surprised that you can even reach the keyboard, still let's press on. The
ComboBox cb = e.Control as ComboBox;
line is not instantiating a new ComboBox, it is declaring a variable of the
ComboBox
type (on the left hand side of the '=') and making it reference (point to) theComboBox
in the cell that you are currently editing. The event that is being handled is theEditingControlShowing
event and theDataGridViewEditingControlShowingEventArgs
passed to it contain a reference to the editing control being used (e.Control
). Because the column we are talking about is aDataGridViewComboBoxColumn
, when you start to edit one of its cells, theDataGridView
control pops up a real liveComboBox
for you to do the edit with (hence theas ComboBox
part). (If it were aDataGridViewTextBoxColumn
the DGV would give you aTextBox
or if it was aDataGridViewCheckBoxColumn
the DGV would give you aCheckBox
.) So the line is saying to the compiler "Please reserve enough space to hold a reference to a ComboBox which I'll be calling 'cb' and put a copy of the pointer from e.Control in there. Thank you!" (please note that it is a very polite line). Basically it just makes it easier to refer tocb.SelectedIndexChanged
for example, rather than:private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if ((ComboBox)e.Control != null) { // first remove event handler to keep from attaching multiple: ((ComboBox)e.Control).SelectedIndexChanged -= cb\_SelectedIndexChanged; // now attach the event handler ((ComboBox)e.Control).SelectedIndexChanged += cb\_SelectedIndexChanged; } }
which we would have to do otherwise. I hope that you'll agree that the first version is easier to read than this one. If it helps, think of
cb
as being a 'magic' PhotoCopy of theComboBox
from the cell you are editing. It is magic because any thing you do to the copy also happens to the original. So when the code says to hook upcolumn1DataGridViewTextBoxColumn_SelectedIndexChanged
to the copy it is also hooking it up to the real one in yourDataGridView
WhendataGridView1_EditingControlShowing
ends,cb
is thrown -
OK I have looked at your picture and I have to say that I am surprised that you can even reach the keyboard, still let's press on. The
ComboBox cb = e.Control as ComboBox;
line is not instantiating a new ComboBox, it is declaring a variable of the
ComboBox
type (on the left hand side of the '=') and making it reference (point to) theComboBox
in the cell that you are currently editing. The event that is being handled is theEditingControlShowing
event and theDataGridViewEditingControlShowingEventArgs
passed to it contain a reference to the editing control being used (e.Control
). Because the column we are talking about is aDataGridViewComboBoxColumn
, when you start to edit one of its cells, theDataGridView
control pops up a real liveComboBox
for you to do the edit with (hence theas ComboBox
part). (If it were aDataGridViewTextBoxColumn
the DGV would give you aTextBox
or if it was aDataGridViewCheckBoxColumn
the DGV would give you aCheckBox
.) So the line is saying to the compiler "Please reserve enough space to hold a reference to a ComboBox which I'll be calling 'cb' and put a copy of the pointer from e.Control in there. Thank you!" (please note that it is a very polite line). Basically it just makes it easier to refer tocb.SelectedIndexChanged
for example, rather than:private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if ((ComboBox)e.Control != null) { // first remove event handler to keep from attaching multiple: ((ComboBox)e.Control).SelectedIndexChanged -= cb\_SelectedIndexChanged; // now attach the event handler ((ComboBox)e.Control).SelectedIndexChanged += cb\_SelectedIndexChanged; } }
which we would have to do otherwise. I hope that you'll agree that the first version is easier to read than this one. If it helps, think of
cb
as being a 'magic' PhotoCopy of theComboBox
from the cell you are editing. It is magic because any thing you do to the copy also happens to the original. So when the code says to hook upcolumn1DataGridViewTextBoxColumn_SelectedIndexChanged
to the copy it is also hooking it up to the real one in yourDataGridView
WhendataGridView1_EditingControlShowing
ends,cb
is thrown -
It would have solved some issues if we knew that you were never getting to the SelectedIndexChanged code. I assumed that you had already handle the EditControlShowing event and added the event handler for the SelectedIndexChanged event. Henry gave you code to do this, but I can try to explain what is happening since you seemed a bit confused about it. When you create the DataGridView, you can specify columns and specify which column within a table or a datasource that that column is linked to. You can also set up what type of control the column is edited with. You can choose a simple TextBox, or you can have other types, for instance the ComboBox type. When the user clicks within a cell, the DataGridView then checks that column to see what type of control to use to allow the user to edit the cell. This is called the EditControl. If the column type was selected to be a ComboBox, then it will use a ComboBox that the DataGridView created to allow the user to edit the value. It creates the control once and then re-uses it. The EditControlShowing event is set off when the control is actually shown. This ComboBox is just a standard ComboBox and has all of the same events and properties as a standard ComboBox. One of the arguments passed during this event is e.Control. This is the control that is being used. It won't always be a ComboBox...it will be whatever type of control that is being used for that column. So, when that control is passed, you then need to add a handler to whichever event you are interested in...in this case, the SelectedIndexChanged event. You also want to make sure to remove the handler afterwards so that it doesn't get called more than once. In Henry's case, he removed it before adding a new one which is perfectly reasonable since the user could change the selection more than once without the EditControl changing. This may be a bit rambly, but I hope it helps with your understanding of what's happening. I wish you luck with this, but I'm still going to say that an internet course without feedback and online tutorials isn't going to get you far. Your company may not be able to afford a programmer, and it may not fit the timetable for this particular project, but I would suggest finding an entry level course on programming at your community college or state university. You can generally do that pretty cheaply and having an instructor that you can ask questions of can help a ton.
Thanks William. I always knew you software guys have big hearts. I have three small children and I think my wife might go crazy if I do that now, but it may be worth the risk... :laugh: I suggested to Henry that I should trade CAD work for software..... :laugh: Anyway, I'll be working on this later and will let you know how it goes, but both you and Henry have made things very clear.
-
OK I have looked at your picture and I have to say that I am surprised that you can even reach the keyboard, still let's press on. The
ComboBox cb = e.Control as ComboBox;
line is not instantiating a new ComboBox, it is declaring a variable of the
ComboBox
type (on the left hand side of the '=') and making it reference (point to) theComboBox
in the cell that you are currently editing. The event that is being handled is theEditingControlShowing
event and theDataGridViewEditingControlShowingEventArgs
passed to it contain a reference to the editing control being used (e.Control
). Because the column we are talking about is aDataGridViewComboBoxColumn
, when you start to edit one of its cells, theDataGridView
control pops up a real liveComboBox
for you to do the edit with (hence theas ComboBox
part). (If it were aDataGridViewTextBoxColumn
the DGV would give you aTextBox
or if it was aDataGridViewCheckBoxColumn
the DGV would give you aCheckBox
.) So the line is saying to the compiler "Please reserve enough space to hold a reference to a ComboBox which I'll be calling 'cb' and put a copy of the pointer from e.Control in there. Thank you!" (please note that it is a very polite line). Basically it just makes it easier to refer tocb.SelectedIndexChanged
for example, rather than:private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if ((ComboBox)e.Control != null) { // first remove event handler to keep from attaching multiple: ((ComboBox)e.Control).SelectedIndexChanged -= cb\_SelectedIndexChanged; // now attach the event handler ((ComboBox)e.Control).SelectedIndexChanged += cb\_SelectedIndexChanged; } }
which we would have to do otherwise. I hope that you'll agree that the first version is easier to read than this one. If it helps, think of
cb
as being a 'magic' PhotoCopy of theComboBox
from the cell you are editing. It is magic because any thing you do to the copy also happens to the original. So when the code says to hook upcolumn1DataGridViewTextBoxColumn_SelectedIndexChanged
to the copy it is also hooking it up to the real one in yourDataGridView
WhendataGridView1_EditingControlShowing
ends,cb
is thrownAlmost there.....maybe. I'm having object type issues: I've cleaned things up and have the following:
private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e)
{string strPrimary = dataGridView1\[e.ColumnIndex, e.RowIndex\].Value.ToString(); Debug.WriteLine(strPrimary); if (strPrimary == "Test1") { //Build a list var dataSource = new List<Units>(); dataSource.Add(new Units() { Unit = "blah1" }); dataSource.Add(new Units() { Unit = "blah2" }); dataSource.Add(new Units() { Unit = "blah3" }); //Setup data binding this.column2DataGridViewComboBox.DataSource = dataSource; this.column2DataGridViewComboBox.DisplayMember = "Unit"; } } private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox cb = e.Control as ComboBox; if (cb != null) { // first remove event handler to keep from attaching multiple: cb.SelectedIndexChanged -= column1DataGridViewComboBox\_SelectedIndexChanged; // now attach the event handler cb.SelectedIndexChanged += column1DataGridViewComboBox\_SelectedIndexChanged; } }
I get "No overload for 'column1DataGridViewComboBox_SelectedIndexChanged' matches delegate 'System.EventHandler'" However, as you probably know, I can change
private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e)
to
private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, EventArgs e)
which fixes that. But I need the DataGridViewCellEventArgs class to access the ColumnIndex and RowIndex properties. Thoughts?
-
Almost there.....maybe. I'm having object type issues: I've cleaned things up and have the following:
private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e)
{string strPrimary = dataGridView1\[e.ColumnIndex, e.RowIndex\].Value.ToString(); Debug.WriteLine(strPrimary); if (strPrimary == "Test1") { //Build a list var dataSource = new List<Units>(); dataSource.Add(new Units() { Unit = "blah1" }); dataSource.Add(new Units() { Unit = "blah2" }); dataSource.Add(new Units() { Unit = "blah3" }); //Setup data binding this.column2DataGridViewComboBox.DataSource = dataSource; this.column2DataGridViewComboBox.DisplayMember = "Unit"; } } private void dataGridView1\_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox cb = e.Control as ComboBox; if (cb != null) { // first remove event handler to keep from attaching multiple: cb.SelectedIndexChanged -= column1DataGridViewComboBox\_SelectedIndexChanged; // now attach the event handler cb.SelectedIndexChanged += column1DataGridViewComboBox\_SelectedIndexChanged; } }
I get "No overload for 'column1DataGridViewComboBox_SelectedIndexChanged' matches delegate 'System.EventHandler'" However, as you probably know, I can change
private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, DataGridViewCellEventArgs e)
to
private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, EventArgs e)
which fixes that. But I need the DataGridViewCellEventArgs class to access the ColumnIndex and RowIndex properties. Thoughts?
For the
SelectedIndexChanged
handler, you don't need the Column and Row because each time the handler gets called it is called by the relevantComboBox
. Therefore you can get at theComboBox
by Casting thesender
parameter. Here we go again. :-Dprivate void column1DataGridViewComboBox\_SelectedIndexChanged(object sender, EventArgs e) { ComboBox cb = sender as ComboBox; string someString = cb.SelectedItem; // OR int cbIndex = cb.SelectedIndex; // and so on }
or, to do it the longer, more difficult to read way
private void column1DataGridViewComboBox\_SelectedIndexChanged(object sender, EventArgs e) { string someString = ((ComboBox)sender).SelectedItem; // OR int cbIndex = ((ComboBox)sender).SelectedIndex; // and so on }
Good luck! :)
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.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.
-
For the
SelectedIndexChanged
handler, you don't need the Column and Row because each time the handler gets called it is called by the relevantComboBox
. Therefore you can get at theComboBox
by Casting thesender
parameter. Here we go again. :-Dprivate void column1DataGridViewComboBox\_SelectedIndexChanged(object sender, EventArgs e) { ComboBox cb = sender as ComboBox; string someString = cb.SelectedItem; // OR int cbIndex = cb.SelectedIndex; // and so on }
or, to do it the longer, more difficult to read way
private void column1DataGridViewComboBox\_SelectedIndexChanged(object sender, EventArgs e) { string someString = ((ComboBox)sender).SelectedItem; // OR int cbIndex = ((ComboBox)sender).SelectedIndex; // and so on }
Good luck! :)
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.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.
Wait, let me take my earplugs out....Oh yes, now I think I can see :-D
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
// first remove event handler to keep from attaching multiple:
cb.SelectedIndexChanged -= column1DataGridViewComboBox_SelectedIndexChanged;
// now attach the event handler
cb.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
}Since DataGridViewEditingControlShowingEventArgs is handling the EditingControlShowing event and will be passing the arguments to the SelectedIndexChanged event handle, the SelectedIndexChanged event cannot have a handling class that is trying to control the same thing at the same time (in this case the ComboBox). Do you concur?
-
Wait, let me take my earplugs out....Oh yes, now I think I can see :-D
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
// first remove event handler to keep from attaching multiple:
cb.SelectedIndexChanged -= column1DataGridViewComboBox_SelectedIndexChanged;
// now attach the event handler
cb.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
}Since DataGridViewEditingControlShowingEventArgs is handling the EditingControlShowing event and will be passing the arguments to the SelectedIndexChanged event handle, the SelectedIndexChanged event cannot have a handling class that is trying to control the same thing at the same time (in this case the ComboBox). Do you concur?
mprice214 wrote:
Since DataGridViewEditingControlShowingEventArgs is handling the EditingControlShowing event and will be passing the arguments to the SelectedIndexChanged event handle
I think that you have slightly misunderstood what is happening here. The
EditingControlShowing
handler does not pass any arguments, it simply tells theComboBox
where to find the code to execute when itsSelectedIndex
changes and regardless of whatever else is going on it will fire off that code when the index does change. Of course it may not execute it instantly, not even modern computers can really do more than one thing at a time. It has just occurred to me that you are actually asking about the '-=' and '+=' bits. They are like that because theSelectedIndexChanged
contains a list of methods that it should execute. If we used the '+=' line only, then each time we did so another instance of thecolumn1DataGridViewComboBox_SelectedIndexChanged
would be added to the list meaning that it would execute once after the first time, twice after the second, three times...... You get the idea. So we first '-=' it to remove the handler from the list, before adding it again. Of course we could test to see if it was there already NOTE** this is not real codeif (!cb.SelectedIndexChanged.List.Contains(column1DataGridViewComboBox_SelectedIndexChanged))
{
cb.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
}oops forgot to close the tag. but the '-=' then '+=' method is quicker. Hope that 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.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.
modified on Wednesday, May 19, 2010 10:31 AM
-
mprice214 wrote:
Since DataGridViewEditingControlShowingEventArgs is handling the EditingControlShowing event and will be passing the arguments to the SelectedIndexChanged event handle
I think that you have slightly misunderstood what is happening here. The
EditingControlShowing
handler does not pass any arguments, it simply tells theComboBox
where to find the code to execute when itsSelectedIndex
changes and regardless of whatever else is going on it will fire off that code when the index does change. Of course it may not execute it instantly, not even modern computers can really do more than one thing at a time. It has just occurred to me that you are actually asking about the '-=' and '+=' bits. They are like that because theSelectedIndexChanged
contains a list of methods that it should execute. If we used the '+=' line only, then each time we did so another instance of thecolumn1DataGridViewComboBox_SelectedIndexChanged
would be added to the list meaning that it would execute once after the first time, twice after the second, three times...... You get the idea. So we first '-=' it to remove the handler from the list, before adding it again. Of course we could test to see if it was there already NOTE** this is not real codeif (!cb.SelectedIndexChanged.List.Contains(column1DataGridViewComboBox_SelectedIndexChanged))
{
cb.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
}oops forgot to close the tag. but the '-=' then '+=' method is quicker. Hope that 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.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.
modified on Wednesday, May 19, 2010 10:31 AM
Henry Minute wrote:
I think that you have slightly misunderstood what is happening here.
That's a surprise. :^) Got it, but also SelectedIndexChanged cannot access the control via DataGridViewCellEventArgs because the control is accessed via EditingControlShowing with the DataGridViewEditingControlShowingEventArgs class, correct?
-
Henry Minute wrote:
I think that you have slightly misunderstood what is happening here.
That's a surprise. :^) Got it, but also SelectedIndexChanged cannot access the control via DataGridViewCellEventArgs because the control is accessed via EditingControlShowing with the DataGridViewEditingControlShowingEventArgs class, correct?
Ah now I understand.
mprice214 wrote:
also SelectedIndexChanged cannot access the control via DataGridViewCellEventArgs because the control is accessed via EditingControlShowing with the DataGridViewEditingControlShowingEventArgs class, correct?
No, that is not quite right. The reason is that each event handler has its own, internally defined, set of parameters, that you have to adhere to. Most event handlers have the
object sender
(in fact I can't think of one off the top of my head that doesn't) after that most also have some form ofEventArgs
and if the internal definition says it has to have a plain old EventArgs, then it will only work with EventArgs. If the definition says it should have anDataGridViewCellEventArgs
then it will only work if you declare it with one of those. SelectedIndexChanged has an EventArgs because it doesn't need to pass any additional information. You know who sent it from thesender
parameter. The fact that the event has fired means that the SelectedIndex of the sender has changed and that is all the information you need to make use of it DataGridViewEditingControlShowingEventArgs holds additional information about where in the DataGridView (Column, Row) the event was fired from.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.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.
-
Ah now I understand.
mprice214 wrote:
also SelectedIndexChanged cannot access the control via DataGridViewCellEventArgs because the control is accessed via EditingControlShowing with the DataGridViewEditingControlShowingEventArgs class, correct?
No, that is not quite right. The reason is that each event handler has its own, internally defined, set of parameters, that you have to adhere to. Most event handlers have the
object sender
(in fact I can't think of one off the top of my head that doesn't) after that most also have some form ofEventArgs
and if the internal definition says it has to have a plain old EventArgs, then it will only work with EventArgs. If the definition says it should have anDataGridViewCellEventArgs
then it will only work if you declare it with one of those. SelectedIndexChanged has an EventArgs because it doesn't need to pass any additional information. You know who sent it from thesender
parameter. The fact that the event has fired means that the SelectedIndex of the sender has changed and that is all the information you need to make use of it DataGridViewEditingControlShowingEventArgs holds additional information about where in the DataGridView (Column, Row) the event was fired from.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.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.
-
Ah now I understand.
mprice214 wrote:
also SelectedIndexChanged cannot access the control via DataGridViewCellEventArgs because the control is accessed via EditingControlShowing with the DataGridViewEditingControlShowingEventArgs class, correct?
No, that is not quite right. The reason is that each event handler has its own, internally defined, set of parameters, that you have to adhere to. Most event handlers have the
object sender
(in fact I can't think of one off the top of my head that doesn't) after that most also have some form ofEventArgs
and if the internal definition says it has to have a plain old EventArgs, then it will only work with EventArgs. If the definition says it should have anDataGridViewCellEventArgs
then it will only work if you declare it with one of those. SelectedIndexChanged has an EventArgs because it doesn't need to pass any additional information. You know who sent it from thesender
parameter. The fact that the event has fired means that the SelectedIndex of the sender has changed and that is all the information you need to make use of it DataGridViewEditingControlShowingEventArgs holds additional information about where in the DataGridView (Column, Row) the event was fired from.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.” Why do programmers often confuse Halloween and Christmas? Because 31 Oct = 25 Dec.
Guess who. I have the following:
private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, EventArgs e)
{ComboBox cb = sender as ComboBox; string strPrimary = cb.SelectedItem.ToString(); Debug.WriteLine(strPrimary); var dataSource = new List<Units>(); switch (strPrimary) { case "1": //Build a list dataSource.Add(new Units() { Unit = "blah1" }); dataSource.Add(new Units() { Unit = "blah2" }); dataSource.Add(new Units() { Unit = "blah3" }); //Setup data binding this.column2DataGridViewComboBox.DataSource = dataSource; this.column2DataGridViewComboBox.DisplayMember = "Unit"; break; case "2": //Build a list dataSource.Add(new Units() { Unit = "blah4" }); dataSource.Add(new Units() { Unit = "blah5" }); dataSource.Add(new Units() { Unit = "blah6" }); //Setup data binding this.column2DataGridViewComboBox.DataSource = dataSource; this.column2DataGridViewComboBox.DisplayMember = "Unit"; break; case "3": //Build a list dataSource.Add(new Units() { Unit = "blah7" }); dataSource.Add(new Units() { Unit = "blah8" }); dataSource.Add(new Units() { Unit = "blah9" }); //Setup data binding this.column2DataGridViewComboBox.DataSource = dataSource; this.column2DataGridViewComboBox.DisplayMember = "Unit"; break; } }
Well, the problem of course as you know is that ComboBox cb points to the second comboBox once I select it. In fact, once I select a value on the first comboBox and then select the second comboBox to select a value, debug.writeline gives me "WindowsFormsApplication1.Form1+Units" (although the 2nd comboBox does get populated with the appropriate list based upon the selection of the first comboBox). If I use an integer instead of strPrimary and set it as cb.SelectedIndex, what I actually get from debug is the index of the selection of the list of the second comboBox (eg if blah5 is selected, I get an integer = 1) followed by an integer