【问题标题】:How to set the exact dropdownlist item that matches a given string?如何设置与给定字符串匹配的确切下拉列表项?
【发布时间】:2018-10-31 19:16:57
【问题描述】:

我正在开发一个 Windows 窗体应用程序,我必须在其中编辑给定记录。该记录还包含一个下拉列表。我想要做的是允许用户编辑下拉列表项以及记录的其余部分。

我已从数据库中查询了用户必须编辑的所需下拉列表项,并将其分配给字符串变量,如下所示:

DataTable dtMName = Products.SelectByManufacturerId(manufacturerId);
               if (dtMName.Rows.Count > 0)
               {
                   foreach (DataRow item in dtMName.Rows)
                   {
                       string manufacturerName = item[0].ToString();
                   }  
               }

现在,在 foreach 循环中(因为返回的数据行只有一个制造商名称,用户稍后将对其进行编辑),我想选择与 string manufacturerName 匹配的下拉列表项。

到目前为止,我已经尝试了以下代码,但没有成功。

childEditProduct.cmbManufacturer.SelectedIndex = childEditProduct.cmbManufacturer.FindString(manufacturerName);

这种选择匹配下拉列表项的方法有什么问题吗?或者有什么有效的方法可以帮助我的情况吗?任何帮助将不胜感激!

【问题讨论】:

    标签: c# winforms dropdown


    【解决方案1】:

    如果您的项目是字符串,请尝试使用 Combobox.SelectedItem:

    childEditProduct.cmbManufacturer.SelectedItem = manufacturerName;
    

    MSDN documentation

    【讨论】:

      【解决方案2】:

      您可能希望以不同的方式使用ComboBox。 就像使用它的 DataSource 和 Id (int) 而不是名称 (string)。 一个例子:how to bind a list to a combobox? (Winforms)

      【讨论】:

      • 我只想将下拉列表的选定文本设置为字符串manufacturerName变量中的内容。
      • 所以在你的代码中把SelectedIndex改成SelectedText
      【解决方案3】:

      我不知道你到底想在这里做什么,但你可以将你的组合框分配给这样的项目:

      childEditProduct.cmbManufacturer.SelectedIndex = 
          childEditProduct.cmbManufacturer.Items.Cast<string>().ToList()
              .FindIndex(x => x == manufacturerName);
      

      【讨论】:

      • 我希望下拉列表的选定文本与我提供的制造商名称匹配。
      • 好吧,如果manufacturerName 存在于combobox.Items 中,那么这应该可以工作。您可以调试代码并查看FindIndex 返回的索引。
      【解决方案4】:
      //use combobox and just set SelectedText
       ComboBox.SelectedText = manufacturerName;
      

      【讨论】:

        【解决方案5】:

        当我使用以下函数在 childForm 加载事件中加载制造商下拉列表时:

        private void LoadManufacturers()
            {
                cmbManufacturer.Items.Clear();
                DataTable dtManufacturers = DataAccess.Select("select manufacturername from tblmanufacturer order by manufacturername asc");
                foreach (DataRow dr in dtManufacturers.Rows)
                {
                    cmbManufacturer.Items.Add(dr[0].ToString());
                }
                //cmbManufacturer.SelectedIndex = 0;
            }
        

        在页面加载事件中加载制造商时,我还使用代码cmbManufacturer.SelectedIndex = 0; 指定下拉列表的选定索引,我稍后将其注释掉。

        完成后,我会在下拉列表的加载事件中重新计算(重新查询)选定的制造商(用户想要更新),如下所示:

        private void frmChildEditProduct_Load(object sender, EventArgs e)
            {
                LoadManufacturers(); // Calling the loadManufacturers function to load all the manufacturers to the dropdown list.
        
                string manufacturerID = lblManufacturerId.Text;
        
                DataTable dtMName = Products.SelectByManufacturerId(manufacturerID);
                        if (dtMName.Rows.Count > 0)
                        {
                            foreach (DataRow manufacturer in dtMName.Rows)
                            {
                                string manufacturerName = manufacturer[0].ToString();
                                cmbManufacturer.SelectedIndex = Convert.ToInt32(cmbManufacturer.FindStringExact(manufacturerName));
                            }
                        }
             }
        

        我的问题解决了。感谢您在 Stackoverflow 上获得的支持。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-09-26
          • 1970-01-01
          • 1970-01-01
          • 2013-04-19
          • 1970-01-01
          • 2016-11-16
          • 2020-10-23
          • 2016-05-16
          相关资源
          最近更新 更多