如何将新值分配到组合框列表中?

How to assign new value into combo box list?

我正在使用 windows CE,我的界面及其内容是根据所选数据库动态生成的。

组合框从数据库 (description table) 中获取值并将所选值 (value table) 保存到不同的 table。插入过程中没有问题。组合框工作得很好。

但是,当我尝试检索插入的数据时,组合框无法显示插入的值。有什么功能可以用来解决这个问题吗?

selectedDescription = dr["value"].ToString().Split(',').ToList<string>(); 

if (dr["type"].ToString() == "multiple") {

        ComboBox combo = new ComboBox();
        combo.DataSource = selectedDescription;
        combo.Width = 60;
        combo.Height = 27;
        combo.Location = new Point(currentX, currentY);
        combo.Tag = Id;

//如果 getVal 不为空(意味着此特定组合有值 selected/inserted),则 combo.DataSource 应该是插入的值。

但是,当我分配combo.Datasource = ds["val"].ToString();时,我无法在设备上打开这个模块。然后我尝试 combo.SelectedValue,但组合框显示 selectedDescription

if (getVal != null) {

    foreach (DataRow ds in getVal.Tables[0].Rows) {

        if (Convert.ToInt32(ds["descId"].ToString()) == combo.Tag)
        {

            combo.SelectedValue = ds["val"].ToString();

        }

我已经为您开发了一个示例来处理您的情况

这里我首先向组合框添加一些数据

   // first databind combobox
    string cboData="1,2,3,4,5";
    comboBox1.DataSource = cboData.Split(',').ToList<string>(); 

现在尝试在数据绑定后添加一些值。 所以首先我会搜索数据如果找到然后设置它但如果找不到然后将它添加到数据源并设置数据。

    int i = comboBox1.FindStringExact("6");
    if (i >= 0)
    {
        comboBox1.Text = "6";
    }
    else
    {
        List<string> str1 = (List<string>)comboBox1.DataSource;
        str1.Add("6");
        comboBox1.DataSource = null;
        comboBox1.DataSource = str1;
        comboBox1.Text = "6";
    }

如果还是代码不清楚再问我。