المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : ComboBoxes and DataGridViews...



C# Programming
04-02-2009, 04:11 AM
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