组合框更新问题

本文关键字:新问题 更新 组合 | 更新日期: 2023-09-27 18:34:39

>更新

我有一个应用程序,它根据从另一个组合框中选择的值更新组合框。例如,州列表组合框将根据从"国家"组合框中选择的国家/地区进行填充。状态组合框的数据源是基于 MySQL 数据库的数据集。问题是,如果首先选择一个国家/地区,则状态组合框加载正常,但是如果我选择另一个国家/地区,则状态组合框填充为:

  System.Data.DataViewManagerListItemTypeDescriptor

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

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

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

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

组合框更新问题

我认为您不能将ComboBox绑定到数据集,因为您无法指定 DataMember。在这种情况下,您应该将 ComboBox.DataSource 设置为有问题的数据表,然后设置显示/值成员:

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

试一试,让我们知道。

具体取决于您更新组合框数据源的方式。

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