2012-02-22 104 views
6

我想在DataGrid的點擊事件上設置comboBox的選定項,但是我不能。我搜索並嘗試了不同的方式,但沒有成功。如何在C#Windows窗體中設置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; 
    } 
+0

您是否嘗試過將.Text/.Value(無法記住哪一個ComboBox使用)設置爲要選擇的項目? – 2012-02-22 13:55:35

+0

您的IF語句是否執行? – Tigran 2012-02-22 13:56:08

+0

使用包含ID,值(任意主鍵),以填補組合數據源類的列表,然後使用SelectedValue屬性:cmbVendor.SelectedValue – 2012-02-22 13:56:56

回答

12

您可以得到由.Items.IndexOf()方法您的項目索引。試試這個:

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

你不需要迭代。

您可以在堆棧溢出問題How do I set the selected item in a comboBox to match my string using C#?中找到更多信息。

+0

抱歉,沒有工作...的indexOf返回-1甚至GridView控件提供了本字符串 – Azhar 2012-02-23 07:37:43

+0

所以,什麼是錯的這個:gridView1.GetFocusedRowCellValue(「vVendor」)。 – Kamil 2012-02-23 18:00:39

4

你在擁有它你的,如果:

cmbVendor.SelectedItem = cmbVendor.Items[i]; 
+1

不再需要循環,直接將.SelectedItem設置爲'Convert.ToString(gridView1.GetFocusedRowCellValue(「vVendor」))'我猜。 – 2012-02-22 13:58:37

+0

抱歉,沒有工作......其實它不是真正的if語句 – Azhar 2012-02-23 07:38:41

1

假設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內容

2

最後我找到了它它是:。

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

的SelectedText屬性是選擇部分可編輯文本在組合框的文本框部分

11

以下內容適用於我完美通過任何值或組合框中可用的文本

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

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

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

這對我的作品.....

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

有人可以告訴我爲什麼我的ans得到了投票嗎? – 2017-11-16 11:06:23

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; 
相關問題