簡體   English   中英

如何在C#Windows窗體中設置ComboBox的選定項?

[英]How to set Selected item of ComboBox in C# Windows Forms?

我正在嘗試在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;
    }

您可以通過.Items.IndexOf()方法獲取商品索引。 嘗試這個:

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

您不需要迭代。

您可以在“堆棧溢出”問題中找到更多信息。 如何使用C#在comboBox中設置所選項目以匹配我的字符串?

以下內容非常適合我。 傳遞組合框中可用的任何值或文本。

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

您是否擁有它?

cmbVendor.SelectedItem = cmbVendor.Items[i];

終於我找到了。 它的:

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

SelectedText屬性用於組合框的文本框部分中可編輯文本的選定部分。

假設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的內容cmbVendor.Items

我有一個類似的問題,並在這里其他答案的幫助下部分解決了。 首先,我的特殊問題是

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

如果您為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;
ComboBox1.SelectedIndex= ComboBox1.FindString("Matching String From DataGrid Cell value")

試試這個,這將在C#Windows應用程序中正常工作

這對我有用.....

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM