C# 组合框更新问题 更新

C# 组合框更新问题 更新,c#,C#,我有一个应用程序,它根据从另一个组合框中选择的值更新组合框。例如,状态列表组合框将根据从“国家”组合框中选择的国家填充。状态组合框的数据源是基于MySQL数据库的数据集。问题是,如果首先选择了一个国家,则状态组合框将正常加载,但如果我选择了另一个国家,则状态组合框将填充以下内容: System.Data.DataViewManagerListItemTypeDescriptor 我研究发现,问题与我无法更新combobox的数据源有关,但我必须这样做,因为值存储在不同的表中,需要不同的查询

我有一个应用程序,它根据从另一个组合框中选择的值更新组合框。例如,状态列表组合框将根据从“国家”组合框中选择的国家填充。状态组合框的数据源是基于MySQL数据库的数据集。问题是,如果首先选择了一个国家,则状态组合框将正常加载,但如果我选择了另一个国家,则状态组合框将填充以下内容:

  System.Data.DataViewManagerListItemTypeDescriptor
我研究发现,问题与我无法更新combobox的数据源有关,但我必须这样做,因为值存储在不同的表中,需要不同的查询。那么,如何清除数据源/解决此问题

我已经更新了帖子,在下面添加了我的代码(第一个是第一个组合框的代码,它根据此处选择的值填充下一个组合框:

  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");
         }
    }
此代码显示填充第二个组合框的部分方法:

 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);
            }.......
这段代码显示了基于对MySQL数据库的查询填充数据集并加载组合框的方法

      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()); }

        } 

    }
此代码是生成另一个数据集的另一个方法

               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()); }
        } 
    }

如前所述,当更改第一个组合框时,我需要帮助使用来自不同数据集的新数据更新组合框,并调用另一个方法。

取决于您如何更新组合框数据源

CountryComboBox_SelectedIndexChanged(...)
{
    string[] states = GetStates(CountryComboBox.SelectedItem.ToString());
    StateComboBox.DataSource = states;
}

这应该行得通。

我会避免将组合框直接绑定到此处的数据集,而是创建一个额外的处理层,以保持UI处于检查状态

下面的伪代码:

所以不是

//bind directly to dataset/query
comboBox.DataSource = dsCountries;
差不多

    public void Load()
    {
        //set up combobox, bind to countries
        ComboBox comboBox = new ComboBox();
        comboBox.DataSource = GetCountries(ds);
        comboBox.DisplayMember = "Name";

        //when comboBox selection changed, load countries based on selected object
        comboBox.SelectedIndexChanged += (o, s) =>
        {
            var countrySelected = (Country)comboBox.SelectedItem;

            //populate countries based on selection
        };
    }

    /// <summary>
    /// Build a list of countries, based upon the rows in a resulting dataset
    /// </summary>
    /// <param name="set"></param>
    /// <returns></returns>
    public List<Country> GetCountries(DataSet set)
    {
        List<Country> result = new List<Country>();

        foreach (DataRow row in set.Tables[0].Rows)
        {
            result.Add(new Country()
            {
                ID = (int)row.ItemArray[0],
                Name = (string)row.ItemArray[1]
            });
        }

        return result;
    }
public void Load()
{
//设置组合框,绑定到国家/地区
ComboBox ComboBox=新建ComboBox();
comboBox.DataSource=GetCountries(ds);
comboBox.DisplayMember=“Name”;
//当组合框选择更改时,根据所选对象加载国家/地区
comboBox.SelectedIndexChanged+=(o,s)=>
{
var countrySelected=(国家)组合框。SelectedItem;
//根据选择填充国家/地区
};
}
/// 
///根据生成的数据集中的行,构建国家列表
/// 
/// 
/// 
公共列表GetCountries(数据集集集)
{
列表结果=新列表();
foreach(集合中的DataRow行。表[0]。行)
{
结果。添加(新国家/地区()
{
ID=(int)行.ItemArray[0],
名称=(字符串)行.ItemArray[1]
});
}
返回结果;
}

我认为您不能将
ComboBox
绑定到数据集,因为您不能指定DataMember。在这种情况下,您应该将ComboBox.DataSource设置为相关的datatable,然后设置display/value成员:

cboProd1.DataSource = ds.Tables["drugs"];
cboProd1.DisplayMember = "code";
cboProd1.ValueMember = "id";

请尝试一下,并让我们知道。

如果可能的话,请给我们您的代码段。我将获取我的代码并将其粘贴到一点……我现在不在我的开发机器上。谢谢。。。