简体   繁体   中英

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

I am trying to set selected item of comboBox on click event of DataGrid, but I could not. I have googled and tried different ways but without success.

For me SelectedIndex is working, but I could not find the index of items in ComboBox, so I could not select the item.

Not working code:

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

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

You can get your item index by the .Items.IndexOf() method. Try this:

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

You don't need to iterate.

You can find more information in Stack Overflow question How do I set the selected item in a comboBox to match my string using C#? .

The following is working for me perfectly. Pass any value or Text which is available in the combobox.

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

您是否拥有它?

cmbVendor.SelectedItem = cmbVendor.Items[i];

At last I found it out. It's:

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

The SelectedText property is for the selected portion of the editable text in the textbox part of the combo box.

Assuming gridView1.GetFocusedRowCellValue("vVendor") really works as expected, the following code should fix the problem.

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

The original code had multiple calls to gridView1.GetFocusedRowCellValue("vVendor") , whereas you only need one.

The suggested "comboBox1.Items.IndexOf(" assumes too much about the content of cmbVendor.Items .

I had a similar problem and worked it out partially with the help of the other answers here. First, my particular problem was that

combobox1.SelectedItem = myItem;

was not working as expected. The root cause was that myItem was an object from a group which was effectively the same list as the items in the combobox, but it was actually a copy of those items. So myItem was identical to a valid entry, but itself was not a valid object from the combobox1 container.

The solution was to use SelectedIndex instead of SelectedItem, like this:

combobox1.SelectedIndex = get_combobox_index(myItem);

where

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

If you have set ValueMember property for the ComboBox control, you can simply assingn the Value to the ComboBox control's SelectedValue property. You don't have to find the index explicitly. Here's an example:

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应用程序中正常工作

this works for me.....

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM