UPDATED
I have an application that updates a combobox based on the values selected from another combobox.E.g List of states combobox will be populated based on the country selected from the “country” combobox.The datasource for the state combobox is a dataset based on a MySQL database. The issue is that if a country is selected first, the state combobox loads fine,but if i select another country,the state combobox is populated with:
System.Data.DataViewManagerListItemTypeDescriptor
I researched and found out that the issue has to do with me being unable to update the datasource of the combobox,but i have to do this because the values are stored in different tables,and require different queries. So, how can i clear the datasource/resolve this issue?.
I have updated the post to add my code below(first is the code for the first combobox which populates the next based on values selected here:
private void cboDisp1_SelectedIndexChanged(object sender, EventArgs e)
{
cboProd1.Enabled = false;
cboProd1.Invalidate();
string dispensation1;
dispensation1 = cboDisp1.SelectedValue.ToString();
if (dispensation1 == "1")
{
}
else
{ cboProd1.Enabled = true;
LoadDispensationCode(dispensation1,"1");
}
}
This code shows part of the method that populates the second combobox:
private void LoadDispensationCode(string text_id,string line_no)
{
string txt_id=text_id;
string ln_no=line_no;
//MessageBox.Show(txt_id, "Value");
txt_id = text_id;
if (txt_id == "1")
{ }
if
(txt_id == "2")
{ LoadConsultation("1","1"); }
if(txt_id=="3")
{
LoadDrug(ln_no);
}.......
This code shows the method that fills the dataset based on the query to the MySQL database, and loads the combobox.
private void LoadDrug(string line_no)
{
string ln_no;
ln_no = line_no;
//MessageBox.Show(ln_no,"value of text");
if (ln_no =="1")
{
try
{
MySqlConnection connection = HopeDB.GetConnection();
String selectStatement = "Select id,code from drugs order by id";
MySqlCommand command = new MySqlCommand(selectStatement, connection);
MySqlDataAdapter adapter = new MySqlDataAdapter();
DataSet ds = new DataSet();
adapter.SelectCommand = command;
adapter.Fill(ds, "drugs");
adapter.TableMappings.Add("Table", "drugs");
cboProd1.DisplayMember = "drugs.code";
cboProd1.ValueMember = "drugs.id";
cboProd1.DataSource = ds;
connection.Close();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message, ex.GetType().ToString()); }
}
}
This code is another method that generates another dataset.
private void LoadVaccine(string line_no)
{
string ln_no = line_no;
if (ln_no == "1")
{
try
{
MySqlConnection connection = HopeDB.GetConnection();
String selectStatement = "Select id,code from vaccines order by id";
MySqlCommand command = new MySqlCommand(selectStatement, connection);
MySqlDataAdapter adapter = new MySqlDataAdapter();
DataSet ds = new DataSet();
adapter.SelectCommand = command;
adapter.Fill(ds, "vaccines");
adapter.TableMappings.Add("Table", "vaccines");
cboProd1.DisplayMember = "vaccines.code";
cboProd1.ValueMember = "vaccines.id";
cboProd1.DataSource = ds;
//cboStateIDFNo.Enabled =false;
//cboVisitType.Enabled =false;
//cboStatus.Enabled = false;
connection.Close();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message, ex.GetType().ToString()); }
}
}
As mentioned earlier,i need help on updating the combobox with fresh data from a different dataset when the first combobox is changed,and another method is called.
I don’t think you can bind
ComboBoxs to a dataset, because you can’t specify the DataMember. That being the case, you should instead set the ComboBox.DataSource to the datatable in question and then set the display/value members:Give that a try and let us know.