|
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":
dataSource.Add(new Units() { Unit = "blah1" });
dataSource.Add(new Units() { Unit = "blah2" });
dataSource.Add(new Units() { Unit = "blah3" });
this.column2DataGridViewComboBox.DataSource = dataSource;
this.column2DataGridViewComboBox.DisplayMember = "Unit";
break;
case "2":
dataSource.Add(new Units() { Unit = "blah4" });
dataSource.Add(new Units() { Unit = "blah5" });
dataSource.Add(new Units() { Unit = "blah6" });
this.column2DataGridViewComboBox.DataSource = dataSource;
this.column2DataGridViewComboBox.DisplayMember = "Unit";
break;
case "3":
dataSource.Add(new Units() { Unit = "blah7" });
dataSource.Add(new Units() { Unit = "blah8" });
dataSource.Add(new Units() { Unit = "blah9" });
this.column2DataGridViewComboBox.DataSource = dataSource;
this.column2DataGridViewComboBox.DisplayMember = "Unit";
break;
}
}<pre>
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 = 0 (I don't know why it is passing that second integer = 0 ??), which then causes the list to be regenerated to the items in case 0. Since ComboBox cb points to the second comboBox, and it passes that second integer = 0, the only allowable entry into the second comboBox would be "blah1"
With all of this, it seems that either close or going about it all wrong, but I don't know enough to make that call. I know that the control for each comboBox needs to be separated, but don't have the slightest notion how to do that. You had mentioned that in a previous post, but in the EditingControlShowing handler. As usual, any suggestions would be greatly appreciated.
|
|
|
|
|
Aaaaand relax!
I think I know what is going on here. If you recall, well even if you don't recall actually , in my second message to you I had this bit of code:
(I've annotated it a bit and modified it v,v slightly)
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb;
switch (e.Column)
{
case 0:
cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectedIndexChanged -= column1DataGridViewComboBox_SelectedIndexChanged;
cb.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
}
break;
}
}
If you can absolutely guarantee that you will only want to do this for one ComboBoxColumn you can simplify the code slightly.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb;
if (e.Column == 0)
{
cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectedIndexChanged -= column1DataGridViewComboBox_SelectedIndexChanged;
cb.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
}
}
}
If you do not check that we are dealing with the correct column before applying the link to the SelectedIndexChanged handler, it will get applied to any and all ComboBoxColumns in the grid.
If this does not resolve the issue, please come back.
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.
|
|
|
|
|
|
mprice214 wrote: DataGridViewEditingControlShowingEventArgs does not contain the "column" method.
That's what you get for trying to code off the top of your head. Good to see that you were smart enough to use the correct form.
Once again I think I have worked out what is happening. In order to save on system resources, dear old M$ are using the same ComboBox for all ComboBoxCell s and just hiding it, rather than destroying it, when not needed. Because it is just hidden, the link to the SelectedIndexChanged never gets deleted, which is why it occurs for column2 as well.
After very little thought I see three ways to handle the problem:
1. in the SelectedIndexChanged handler test for the column index before anything else and bomb out (a technical term ) if not the one we want.
private void column1DataGridViewComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex != 0)
{
return;
}
ComboBox cb = sender as ComboBox;
int intPrimary = cb.SelectedIndex;
Debug.WriteLine(intPrimary + "SelectedIndexChanged");
................................
................................
................................
................................
}
2. in the EditingControlShowing handler remove the link when we don't find the column we want.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb;
Debug.WriteLine(dataGridView1.CurrentCell.ColumnIndex + "EditControlShowing");
switch (dataGridView1.CurrentCell.ColumnIndex)
{
case 0:
cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectedIndexChanged -= column1DataGridViewComboBox_SelectedIndexChanged;
cb.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
}
break;
default:
cb.SelectedIndexChanged -= column1DataGridViewComboBox_SelectedIndexChanged;
break;
}
}
3. handle the EditModeChanged event and remove the link there. This will entail one more change to existing code.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb;
Debug.WriteLine(dataGridView1.CurrentCell.ColumnIndex + "EditControlShowing");
switch (dataGridView1.CurrentCell.ColumnIndex)
{
case 0:
cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectedIndexChanged -= column1DataGridViewComboBox_SelectedIndexChanged;
cb.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
}
break;
}
}
becomes
ComboBox hookedCombo;
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
Debug.WriteLine(dataGridView1.CurrentCell.ColumnIndex + "EditControlShowing");
switch (dataGridView1.CurrentCell.ColumnIndex)
{
case 0:
hookedCombo = e.Control as ComboBox;
if (hookedCombo != null)
{
hookedCombo.SelectedIndexChanged += column1DataGridViewComboBox_SelectedIndexChanged;
}
break;
}
}
Then add the EditModeChanged handler
private void dataGridView1_EditModeChanged(object sender, EventArgs e)
{
if (hookedCombo != null)
{
hookedCombo.SelectedIndexChanged -= OnCountryComboChanged;
}
hookedCombo = null;
}
I think #3 is technically the more correct way to do it.
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.
|
|
|
|
|
Thanks for your help on this. I was able to become much more familiar with the event handlers.
I began focusing on DataGridView FAQ Appendix A, 18 that you mentioned a bit ago. I was trying to get that going and was still having some issues and then came across
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/6f6c2632-afd7-4fe9-8bf3-c2c8c08d1a31/[^]
Anyway, I'll have to tweak it a bit to keep users from pulling down the second comboBox first (which will throw and exception), but it's exactly what I need. Anyway, you taking the time really helped me out to understand some "basic" things that will help me get through the rest of this project.
Thanks again.
public partial class Form1 : Form
{
DataTable tblPrimary, tblSecondary;
BindingSource primaryBS, filteredSecondaryBS, unfilteredSecondaryBS;
public Form1()
{
tblPrimary = new DataTable("Primary");
tblPrimary.Columns.Add("ID", typeof(int));
tblPrimary.Columns.Add("Name", typeof(string));
tblSecondary = new DataTable("Secondary");
tblSecondary.Columns.Add("ID", typeof(int));
tblSecondary.Columns.Add("subID", typeof(int));
tblSecondary.Columns.Add("Name", typeof(string));
tblPrimary.Rows.Add(new object[] { 0, "Force" });
tblPrimary.Rows.Add(new object[] { 1, "Torque" });
tblPrimary.Rows.Add(new object[] { 2, "Pressure" });
tblSecondary.Rows.Add(new object[] { 0, 0, "lb" });
tblSecondary.Rows.Add(new object[] { 1, 0, "N" });
tblSecondary.Rows.Add(new object[] { 2, 0, "oz" });
tblSecondary.Rows.Add(new object[] { 3, 1, "in-lb" });
tblSecondary.Rows.Add(new object[] { 4, 1, "ft-lb" });
tblSecondary.Rows.Add(new object[] { 5, 1, "N-m" });
tblSecondary.Rows.Add(new object[] { 6, 2, "PSI" });
tblSecondary.Rows.Add(new object[] { 7, 2, "Pa" });
tblSecondary.Rows.Add(new object[] { 8, 2, "bar" });
InitializeComponent();
primaryBS = new BindingSource();
primaryBS.DataSource = tblPrimary;
primaryComboBoxColumn.DataSource = primaryBS;
primaryComboBoxColumn.DisplayMember = "Name";
primaryComboBoxColumn.ValueMember = "ID";
unfilteredSecondaryBS = new BindingSource();
DataView undv = new DataView(tblSecondary);
unfilteredSecondaryBS.DataSource = undv;
secondaryComboBoxColumn.DataSource = unfilteredSecondaryBS;
secondaryComboBoxColumn.DisplayMember = "Name";
secondaryComboBoxColumn.ValueMember = "ID";
filteredSecondaryBS = new BindingSource();
DataView dv = new DataView(tblSecondary);
filteredSecondaryBS.DataSource = dv;
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.ColumnIndex == secondaryComboBoxColumn.Index)
{
DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
dgcb.DataSource = filteredSecondaryBS;
this.filteredSecondaryBS.Filter = "subid = " +
this.dataGridView1[e.ColumnIndex - 1, e.RowIndex].Value.ToString();
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == this.secondaryComboBoxColumn.Index)
{
DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];
dgcb.DataSource = unfilteredSecondaryBS;
this.filteredSecondaryBS.RemoveFilter();
}
}
|
|
|
|
|
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.
|
|
|
|
|
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...
I suggested to Henry that I should trade CAD work for software.....
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.
|
|
|
|
|
I have Window service that is hosting a Remote Interface and Remote Object. I am using Visual Studio 2008. I have Windows Applications client that gets the Remote Service , object and use their methods and variables just fine but when I get from the Windows Service the Remote Object and one of its methods it says that requested service is not found. Does anyone have I clue or reason why this is happening??
|
|
|
|
|
I think , you use TCPIP remoting.
Control your windows service is running stable and than if server ,which windows service running on,firewall program is on than open related port that your application use for remoting.
|
|
|
|
|
Yeah, I always forget Critical details I using TCPIP remoting. That was the first thing that i thought of but there is no firewall on the server. My hosting Service is the one that can't access the remote Object.
|
|
|
|
|
Is your uri or class name correct?
|
|
|
|
|
Hi all,
I want to change the default paper size in internet explorer and print preview...
For example I want to have A5 size when some page in going to be previewed...
Please help me.
thanks.
Every new thing you learn,Gives you a new personality.
|
|
|
|
|
What does that have to do with C#???
Everything makes sense in someone's mind
|
|
|
|
|
Well, you'd have to see sharp to read an entire web page when printed on an A5.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Weak. Very weak.
Everything makes sense in someone's mind
|
|
|
|
|
Hi.
I´ve been googling for a while, but cant find the right place to start, so I thought some of you people could point me in the right direction
I need to retrieve, add and edit Outlook 2007 calendar information. This information should be accessed/edited from a standalone C# application, and has therefore nothing to do with a Outlook plugin.
So do i need to use VSTO? Or where do I start?
Thanks alot!
|
|
|
|
|
I think that Outlook Interop[^] is probably where you need to start.
I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in.
Apathy Error: Don't bother striking any key.
|
|
|
|
|
Thanks alot!
|
|
|
|
|
I have an HTML file in a local directory which will have an a tag
I want to remove the checked part from the above tag & save the file back from my c# desktop application so that whoever accesses the html will not see the checkbox checked by default.
I dont want to use 3rd part parsers like Html Agility Pack. I have been struggling with Microsoft.mshtml & DOM in loading the HTML, getting the Tag by Id.
Appreciate your help.
Thanks!!
|
|
|
|
|
Try loading it into a System.XML.XMLDocument . You should be able to find the tag using SelectSingleNode , update the node and save the XMLDocument back to the FileSystem.
|
|
|
|
|
had tried to load using XMLDoc but it fails to load because of html xml diffrences ..
|
|
|
|
|
Are you creating the HTML in the first place? It is certainly possible to make HTML pages that are valid XML. Start with checked="checked". All attributes should have quoted values.
Check out DTDs[^] and the W3 Validator[^]
|
|
|
|
|
As you found you can't treat it as XML and use an XML parser. Though it is brute force, the best method may be to simple read the contents into a string and use string.replace.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|