ComboBoxes and DataGridViews...
-
Hi, I have a form that contains a ComboBox and a DataGridView. The ComboBox gets populated with all the countries as seen in the code below:
private void PopulateCountries() { cbCountryId.DataSource = dsCountries.Tables\["Country"\]; cbCountryId.DisplayMember = "Country"; cbCountryId.ValueMember = "Id"; }
What I need to do is populate the DataGridView based on what country the user select. For example: if you select Australia then all the Australian states should display in the gridview. The code to display the states look like this:
private void GetStateByCountryId(int CountryId) { this.dsWBGT\_V4 = dataCommunicator.GetStateByCountryId(CountryId); this.stateBindingSource.DataSource = this.dsWBGT\_V4; }
As you can see I have a Property named CountryId but I cannot put it in the SelectedIndexChanged event because if the application start the event fires and I get an error because there was no selection in the ComboBox. I set the Property with this line:
CountryId = Convert.ToInt32(cbCountryId.SelectedValue);
Can anyone tell me how can I get the ComboBox to display a value by default at first run and set the property equal to that property so that the states in the selected country displays??
Illegal Operation
-
Hi, I have a form that contains a ComboBox and a DataGridView. The ComboBox gets populated with all the countries as seen in the code below:
private void PopulateCountries() { cbCountryId.DataSource = dsCountries.Tables\["Country"\]; cbCountryId.DisplayMember = "Country"; cbCountryId.ValueMember = "Id"; }
What I need to do is populate the DataGridView based on what country the user select. For example: if you select Australia then all the Australian states should display in the gridview. The code to display the states look like this:
private void GetStateByCountryId(int CountryId) { this.dsWBGT\_V4 = dataCommunicator.GetStateByCountryId(CountryId); this.stateBindingSource.DataSource = this.dsWBGT\_V4; }
As you can see I have a Property named CountryId but I cannot put it in the SelectedIndexChanged event because if the application start the event fires and I get an error because there was no selection in the ComboBox. I set the Property with this line:
CountryId = Convert.ToInt32(cbCountryId.SelectedValue);
Can anyone tell me how can I get the ComboBox to display a value by default at first run and set the property equal to that property so that the states in the selected country displays??
Illegal Operation
In your selectedindexchanged event test that there is a valid item
if(DGV.SelectedItems.count == 0) {return ;}
This is a common issue - an index changes twice - deselects on [0] and selects[4] so the event fires twice. This is needed for all collection controls, you just need to find the correct property to test for each control type.Never underestimate the power of human stupidity RAH
-
In your selectedindexchanged event test that there is a valid item
if(DGV.SelectedItems.count == 0) {return ;}
This is a common issue - an index changes twice - deselects on [0] and selects[4] so the event fires twice. This is needed for all collection controls, you just need to find the correct property to test for each control type.Never underestimate the power of human stupidity RAH
Thank you for the reply. Excuse my ignorance but I have tried to add this code to the IndexChange event but I have no option of SelectedItems.count? I have tried dgvState.SelectedRows.Count but obviously that does not work?
Illegal Operation
-
Hi, I have a form that contains a ComboBox and a DataGridView. The ComboBox gets populated with all the countries as seen in the code below:
private void PopulateCountries() { cbCountryId.DataSource = dsCountries.Tables\["Country"\]; cbCountryId.DisplayMember = "Country"; cbCountryId.ValueMember = "Id"; }
What I need to do is populate the DataGridView based on what country the user select. For example: if you select Australia then all the Australian states should display in the gridview. The code to display the states look like this:
private void GetStateByCountryId(int CountryId) { this.dsWBGT\_V4 = dataCommunicator.GetStateByCountryId(CountryId); this.stateBindingSource.DataSource = this.dsWBGT\_V4; }
As you can see I have a Property named CountryId but I cannot put it in the SelectedIndexChanged event because if the application start the event fires and I get an error because there was no selection in the ComboBox. I set the Property with this line:
CountryId = Convert.ToInt32(cbCountryId.SelectedValue);
Can anyone tell me how can I get the ComboBox to display a value by default at first run and set the property equal to that property so that the states in the selected country displays??
Illegal Operation
you can ignore the selectedIndexChanged Event whenever you want. follow the steps to ignore the event and put it back when the form completly load. comboBox1.SelectedIndexChanged -=comboBox1_selectedIndexChanged; do your work here and then comboBox1.SelectedIndexChanged +=comboBox1_selectedIndexChanged; "comboBox1_selectedIndexChanged" is the selected Index Change event of comboBox1