【问题标题】:How to assign new value into combo box list?如何将新值分配到组合框列表中?
【发布时间】:2015-07-20 11:07:49
【问题描述】:

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

组合框从数据库(description table) 中获取值并将选定的值(value 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 不为空(意味着为此特定组合选择/插入了值),则 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();

        }

【问题讨论】:

  • 看来你必须通过这样写来控制页面回发: if (Page.IsPostBack) { return;因为回发后,会看到ComboBox的空白数据。
  • 是windows form还是webform相关的问题?
  • 你能演示/修改我的代码@hqtunes.com吗?
  • 这是windows窗体相关的@Mou
  • 再次简要告诉我问题......所以我可以试试!

标签: c# combobox


【解决方案1】:

我已经为你开发了一个样本来处理你的情况

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

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

如果代码仍然不清楚,请询问我。

【讨论】:

  • 除了 FindStringExact 函数,我还能用什么函数?
  • 您在使用“FindStringExact”函数时遇到了什么问题?
  • 最好在 MSDN 中搜索组合框,在那里你可以看到所有与搜索相关的功能。
  • 我的组合框没有“FindStringExact”功能。好的,谢谢!!
  • 您使用的是哪个 dotnet 版本?我用的是 dotnet 4.0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多