【问题标题】:How to set Selected item of ComboBox in C# Windows Forms?如何在 C# Windows 窗体中设置 ComboBox 的选定项?
【发布时间】:2012-03-12 20:36:45
【问题描述】:

我试图在 DataGrid 的单击事件上设置 comboBox 的选定项,但我不能。我用谷歌搜索并尝试了不同的方法,但没有成功。

对我来说SelectedIndex 正在工作,但我在 ComboBox 中找不到项目的索引,所以我无法选择项目。

不工作的代码:

for (int i = 0; i < cmbVendor.Items.Count; i++)

    if (cmbVendor.Items[i].ToString() == Convert.ToString(gridView1.GetFocusedRowCellValue("vVendor")))
    {
        cmbVendor.SelectedIndex = i;
        break;
    }

【问题讨论】:

  • 您是否尝试将 .Text/.Value(不记得使用哪个 ComboBox)设置为您要选择的项目?
  • 使用包含 id、值(任何主键)的类列表来填充组合数据源,然后使用 selectedvalue 属性:cmbVendor.SelectedValue
  • 如果在 selectedIndex 中获取值 true 和问题,还是如果始终为 false?
  • GetFocusedRowCellValue() 不是 DataGrid 的方法。如果您使用其他供应商的网格控件或使用扩展方法,那么您必须在问题中记录。

标签: c# .net winforms combobox


【解决方案1】:

您可以通过.Items.IndexOf() 方法获取您的项目索引。试试这个:

comboBox1.SelectedIndex = comboBox1.Items.IndexOf(gridView1.GetFocusedRowCellValue("vVendor"));

你不需要迭代。

您可以在 Stack Overflow 问题中找到更多信息How do I set the selected item in a comboBox to match my string using C#?

【讨论】:

  • 抱歉不工作......即使gridview提供了当前字符串,indexOf也返回-1
  • 所以这里有问题:gridView1.GetFocusedRowCellValue("vVendor")。
【解决方案2】:

以下内容非常适合我。传递组合框中可用的任何值或文本。

comboBox1.SelectedIndex = comboBox1.FindString(<combobox value OR Text in string formate>);

【讨论】:

    【解决方案3】:

    你的 if 中有它:

    cmbVendor.SelectedItem = cmbVendor.Items[i];
    

    【讨论】:

    • 不再需要循环,直接将 .SelectedItem 设置为 'Convert.ToString(gridView1.GetFocusedRowCellValue("vVendor"))' 我猜。
    • 对不起不工作......其实if语句不正确
    【解决方案4】:

    我终于找到了。它是:

    cmbVendor.Text = Convert.ToString(gridView1.GetFocusedRowCellValue("vVendor"));
    

    SelectedText 属性用于组合框文本框部分中可编辑文本的选定部分。

    【讨论】:

      【解决方案5】:

      如果您为 ComboBox 控件设置了 ValueMember 属性,您可以简单地将 Value 分配给 ComboBox 控件的 SelectedValue 属性。您不必显式查找索引。 这是一个例子:

      public class Vendor{
          public int VendorId {get; set;}
          public string VendorName {get; set;}
      }
      
      // Inside your function
         var comboboxData = new List<Vendor>(){
             new Vendor(){ vendorId = 1, vendorName = "Vendor1" },
             new Vendor(){ vendorId = 2, vendorName = "Vendor2" }
         }
      
         cmbVendor.DataSource = comboboxData;
         cmbVendor.DisplayMember = "VendorName";
         cmbVendor.ValueMember = "ValueId";
      
      // Now, to change your selected index to the ComboBox item with ValueId of 2, you can simply do:
         cmbVendor.SelectedValue = 2;
      

      【讨论】:

      • 使用此方法选择项目可以正常工作。但是,实际上没有一个答案通过执行cmb.DroppedDown=true; 在显示列表时突出显示所选项目
      【解决方案6】:

      假设gridView1.GetFocusedRowCellValue("vVendor") 确实按预期工作,以下代码应该可以解决问题。

      string selected = Convert.ToString(gridView1.GetFocusedRowCellValue("vVendor"));
      foreach ( var item in cmbVendor.Items )
      {
          if (string.Compare(item.ToString(), selected, StringComparison.OrdinalIgnoreCase) == 0)
          {
              cmbVendor.SelectedItem = item;
              break;
          }
      }
      

      原始代码多次调用gridView1.GetFocusedRowCellValue("vVendor"),而您只需要一次。

      建议的“comboBox1.Items.IndexOf(”过多地假设cmbVendor.Items的内容。

      【讨论】:

        【解决方案7】:

        我遇到了类似的问题,并在此处的其他答案的帮助下部分解决了。首先,我的特殊问题是

        combobox1.SelectedItem = myItem;
        

        没有按预期工作。根本原因是 myItem 是组中的一个对象,它实际上是与组合框中的项目相同的列表,但它实际上是这些项目的副本。所以 myItem 与有效条目相同,但它本身不是来自 combobox1 容器的有效对象。

        解决方案是使用 SelectedIndex 而不是 SelectedItem,如下所示:

        combobox1.SelectedIndex = get_combobox_index(myItem);
        

        在哪里

            private int get_combobox_index(ItemClass myItem)
            {
                int i = 0;
                var lst = combobox1.Items.Cast<ItemClass >();
                foreach (var s in lst)
                {
                    if (s.Id == myItem.Id)
                        return i;
        
                    i++;
                }
                return 0;
            }
        

        【讨论】:

          【解决方案8】:
          ComboBox1.SelectedIndex= ComboBox1.FindString("Matching String From DataGrid Cell value")
          

          试试这个,这将在 C# Windows 应用程序中正常工作

          【讨论】:

            【解决方案9】:

            这对我有用.....

            string displayMember = ComboBox.DataSource.To<DataTable>().Select("valueMemberColumn = '" + value + "'")[0]["displayMember"].ToString();
            ComboBox.FindItemExact(displayMember, true).Selected = true;
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-01-29
              • 2014-06-26
              • 2013-08-12
              • 2013-11-13
              相关资源
              最近更新 更多