two combobox firing an event for same function - c#

There are two comboboxes. one is created by drag&drop and set event SelectedIndexChanged. And 2nd combobox is created manually, but not given any event. But when I change 2nd combo, it's firing an event for 1st combo's function.
Form prompt = new Form();
prompt.Width = 300;
prompt.Height = 150;
ComboBox cmBox = new ComboBox() { Left = 70, Top = 24, Width = 100, Height=150 };
cmBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
cmBox.ValueMember = "value";
cmBox.DisplayMember = "text";
prompt.ShowDialog();
I tried, but not working:
cmBox.SelectedIndexChanged -= new System.EventHandler(comboBox1_SelectedIndexChanged);
P.S: They are using same bindsource.

If they're sharing a BindingSource, then changing the value in one control will change the value in the other control as well.
When you change the value in the second ComboBox, the value in the first ComboBox changes too, which fires its SelectedIndexChanged event.
Create a separate BindingSource for each control, or if it's a collection, try just assigning the collection directly to each ComboBox.

Related

Two ComboBox with same DataSource causes Selection to be forgotten

I have BindingSource defined:
public System.Windows.Forms.BindingSource bsContractors;
this.bsContractors.DataSource = typeof(Contractor);
and then a ComboBox with a DataSource defined like so:
private System.Windows.Forms.ComboBox cmbConstructionContractors1;
this.cmbConstructionContractors1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bsProject, "Id", true));
this.cmbContractors1.DataSource = this.bsContractors;
this.cmbContractors1.DisplayMember = "Name";
this.cmbContractors1.ValueMember = "Id";
this.cmbContractors1.SelectedIndexChanged += new System.EventHandler(this.cmbContractor1Selected);
This works fine.
I have another ComboBox defined on another Form using the same DataSource:
this.cmbContractorName2.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", myView.bsProject, "Id", true));
this.cmbContractorName2.DataSource = projectView.bsContractors;
this.cmbContractorName2.ValueMember = "Id";
this.cmbContractorName2.DisplayMember = "Name";
this.cmbContractorName2.SelectedIndexChanged += new System.EventHandler(this.cmbContractor2Selected);
When this 2nd ComboBox is displayed, the first ComboBox, which has something selected, gets reset to the first entry, which is blank.
If I pull down on the first ComboBox, the list is still there, it just 'forgot' which one was selected.
Edit: I've discovered that when displaying the 2nd ComboBox, the EventHandler of the 1st ComboBox1 somehow gets assigned to cmbContractors2Selected instead of the original cmbContractors1Selected
Try giving it its own binding object:
this.cmbContractorName2.DataSource = new BindingSource(projectView.bsContractors, null);
This will separate the currency managers.

DataGridView not displaying changes to checkbox when application is launched

I have the following code that will create a checkbox column, insert as first column to the main data grid, and then loop through the rows to set the checkbox to checked. Basically, what I'm trying to do is add checkboxes that are checked by default when the application launches.
The issue is that when the application is started, the checkboxes remain untouched. I've added the ToolTip text below to see whether that takes effect, but no luck there.
I also added an event that will trigger the same code below (calling the same method), and it will refresh the grid with the checkboxes CHECKED.
DataGridViewCheckBoxColumn importSelectionColumn = new DataGridViewCheckBoxColumn();
importSelectionColumn.Name = "dataSelection";
importSelectionColumn.DisplayIndex = 0;
importSelectionColumn.HeaderText = "\u2611";
importSelectionColumn.Width = 35;
importSelectionColumn.Visible = true;
importSelectionColumn.FalseValue = false;
importSelectionColumn.TrueValue = true;
importSelectionColumn.HeaderCell.Style.Font = new Font(FontFamily.GenericSansSerif, 16f);
// Add column to grid:
mainDataGrid.Columns.Insert(0, importSelectionColumn);
// Set checkbox to true for all rows:
foreach (DataGridViewRow row in this.mainDataGrid.Rows)
{
row.Cells["dataSelection"].Value = true;
// Adding this just to see whether it's set when application starts.
row.Cells["dataSelection"].ToolTipText = "Testing";
}
mainDataGrid.RefreshEdit();
mainDataGrid.Refresh();
Make sure that the code that changes state is not being executed too early.
It should be executed after Loaded event of the container form, when all controls are loaded and ready for work.
Adding a check box control to your mainDataGrid can be done in DataBound event instead of on page load event.
Try to use below code on your page and check:
protected void mainDataGrid_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow objRow in mainDataGrid.Rows)
{
TableCell tcCheckCell = new TableCell();
CheckBox chkCheckBox = new CheckBox();
tcCheckCell.Controls.Add(chkCheckBox);
objRow.Cells.AddAt(0, tcCheckCell);
}
}

how to create combobox onchange with programmatically

basically, i create a combobox from toolbox and drag it into Form. but this time, i create a combobox with programmatically like this..
ComboBox filterKeyComboBox = new ComboBox() { Left = 100, Top = 22, Width = 150, DropDownStyle = ComboBoxStyle.DropDownList, DataSource = null };
then i fill it with this..
filterKeyComboBox.Items.Clear();
bs.DataSource = fieldTable;
filterKeyComboBox.DataSource = bs;
filterKeyComboBox.DisplayMember = "Value";
then i want to create eventhandler where every i change selected item from that combobox...
i tried like this..
filterAktifComboBox.SelectedIndexChanged += (sender, e) =>
{
Console.WriteLine("Test onchange selected item");
};
i dont know why, thats "Test onchange selected item" not printed in log.
how to create event item change in combobox with programmatically?
What exactly is "filterAktifComboBox" that you are giving the event?
Shouldn't you be doing it like this:
filterKeyComboBox.SelectedIndexChanged += (sender, e) =>
{
Console.WriteLine("Test onchange selected item");
};
Wich sets the event to
filterKeyComboBox

ComboBox data binding

I have a combobox control on form that pull its data (Displays and values) from some datasource. On another side I have table with one row. I want when app is lauching, combobox set selectedvalue or selecteditem to value of one column in above row. And when user has changed combobox it will persist change to row. I have tried to bind SelectedValue to this column, but it doesn't work. Combobox just sets on start to first item. What is problem?
EDIT
This is a Win Forms project.
Here is the binding code:
this.comboBoxCountries = new System.Windows.Forms.ComboBox();
this.countriesBindingSource = new System.Windows.Forms.BindingSource(this.components);
//
// comboBoxCountries
//
this.comboBoxCountries.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.searchCriteriaBindingSource, "Postcode", true));
this.comboBoxCountries.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.searchCriteriaBindingSource, "CountryCode", true));
this.comboBoxCountries.DataSource = this.countriesBindingSource;
this.comboBoxCountries.DisplayMember = "Name";
this.comboBoxCountries.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxCountries.FormattingEnabled = true;
this.comboBoxCountries.Location = new System.Drawing.Point(190, 19);
this.comboBoxCountries.Name = "comboBoxCountries";
this.comboBoxCountries.Size = new System.Drawing.Size(156, 21);
this.comboBoxCountries.TabIndex = 2;
this.comboBoxCountries.ValueMember = "Code";
this.comboBoxCountries.SelectedValueChanged += new System.EventHandler(this.comboBoxCountries_SelectedValueChanged);
//
// countriesBindingSource
//
this.countriesBindingSource.DataMember = "Countries";
this.countriesBindingSource.DataSource = this.dbDataSetCountries;
//
// dbDataSetCountries
//
this.dbDataSetCountries.DataSetName = "dbDataSetCountries";
this.dbDataSetCountries.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
//
// searchCriteriaBindingSource
//
this.searchCriteriaBindingSource.AllowNew = false;
this.searchCriteriaBindingSource.DataMember = "SearchCriteria";
this.searchCriteriaBindingSource.DataSource = this.dbDataSetSearchCriteria;
this.searchCriteriaBindingSource.BindingComplete += new System.Windows.Forms.BindingCompleteEventHandler(this.searchCriteriaBindingSource_BindingComplete);
//
// dbDataSetSearchCriteria
//
this.dbDataSetSearchCriteria.DataSetName = "dbDataSetSearchCriteria";
this.dbDataSetSearchCriteria.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
EDIT2
As I have mentioned in my comment below, I have another textbox that is binded to another DataMember of same binding source and textbox working fine. It's appear with appropriate value. When I change DataMember on same datamember on which I set selectedvalue property of combobox binding it's also show a good result and work properly.
Thanks in advance!
Take a look at the DisplayMember and ValueMember properties of the combobox. You need to tell the ComboBox what member from the datasource to display in the drop down, and what value to give when SelectedValue is requested.
It sounds like your ComboBox is bound to a static list while your rows are not. You might consider using a BindingSource that you set the ComboBox and the DataGridView's DataSource to. That way when the DGV navigates to a new row, the ComboBox will be updated with the value for the new row.
Here is a link to the ComboBox on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx
I find it out. So for managing with this issue you should remove SelectedValue databinding from visual studio data bound menu and put an appropriate code to add this databinding in some place after filling all bindingsources:
private void MainForm_Load_1(object sender, EventArgs e)
{
this.searchCriteriaTableAdapter1.Fill(this.dbDataSetCountries.SearchCriteria);
this.searchCriteriaTableAdapter.Fill(this.dbDataSetSearchCriteria.SearchCriteria);
comboBoxCountries.DataBindings.Add("SelectedValue", this.dbDataSetCountries.SearchCriteria, "CountryCode");
}

How to set DataGridView's Height properties if the control is inside a ToolStripControlHost? C# WINFORMS

I have a TextBox that will show a DataGridView within a contextMenuStrip everytime the user presses the F1 key. I used ToolStripControlHost to host the dataGridView inside the contextMenuStrip. Please consider my code:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.F1)
{
dataGridView1.BindingContext = this.BindingContext;
dataGridview1.Height = 30;
dataGridView1.DataSource = dt; // some DataTable with 50+ rows,..or greater.
ToolStripControlHost tsHost = new ToolStripControlHost(dataGridView1);
contextMenuStrip1.Items.Clear();
contextMenuStrip1.Items.Add(tsHost);
contextMenuStrip1.Show(textBox1, 0, 27);
}
}
My problem is I can't set programmatically the height of the dataGridView when it's already added in the contextMenuStrip as an item. Since I can't set the height of the grid, the tendency is it adjusts its height depending on the size of its dataSource.
I'm missing something in my code? Please help.. thanks.
You can't change height of your dataGridView1 directly because it's inside other controls - therefore it's height is limited by the size of it's parent controls. If you want to change it's height, you should instead change the heights of contextMenuStrip1 and tsHost. I prefer doing it like this:
dataGridView1.Dock = DockStyle.Fill;
tsHost.Dock = DockStyle.Fill;
contextMenuStrip1.Height = 30;
If it doesn't work, try finding contextMenuStrip1's parent controls and change their heights.
I already figured it out. I just added this code:
ToolStripControlHost tsHost = new ToolStripControlHost(dataGridView1);
tsHost.AutoSize = false; // Set AutoSize property to false.
tsHost.Height = 30; // then set Height property value.
contextMenuStrip1.Items.Clear();
contextMenuStrip1.Items.Add(tsHost);

Categories