How to set a selected ComboBox item in C # Windows Forms?

I am trying to set the selected comboBox element in a DataGrid click event, but failed. I have googled and tried differently but unsuccessfully.

For me, SelectedIndex works, but I could not find the index of the elements in the ComboBox, so I could not select the element.

The code does not work:

 for (int i = 0; i < cmbVendor.Items.Count; i++) if (cmbVendor.Items[i].ToString() == Convert.ToString(gridView1.GetFocusedRowCellValue("vVendor"))) { cmbVendor.SelectedIndex = i; break; } 
+6
source share
9 answers

You can get your product index using the .Items.IndexOf() method. Try the following:

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

You do not need iteration.

You can find more information in the stack overflow question. How to set the selected item in comboBox to match my string using C #? .

+14
source

The following works fine for me. Pass any value or text available in the combo box.

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

You have this in your if:

 cmbVendor.SelectedItem = cmbVendor.Items[i]; 
+4
source

I finally figured it out. It:

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

The SelectedText property for the selected part of the edited text in the text box in the combo box.

+2
source

Assuming gridView1.GetFocusedRowCellValue("vVendor") does work 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; } } 

There were several calls to gridView1.GetFocusedRowCellValue("vVendor") in the source code, while you only need one.

The proposed "comboBox1.Items.IndexOf (" says too much about the contents of cmbVendor.Items .

+1
source

I had a similar problem and partially worked it out using the other answers here. Firstly, my particular problem was that

 combobox1.SelectedItem = myItem; 

did not work as expected. The main reason was that myItem was an object from the group, which was actually the same list as the elements in the combobox, but in fact it was a copy of these elements. Thus, myItem was identical to the actual entry, but was itself an invalid object from the combobox1 container.

The solution was to use SelectedIndex instead of SelectedItem, for example:

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

If you set the ValueMember property for the ComboBox control, you can simply define the Value property for the SelectedValue property of the ComboBox control. You do not have to explicitly search for this index. Here is 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; 
0
source
 ComboBox1.SelectedIndex= ComboBox1.FindString(gridView1.GetFocusedRowCellValue("vVendor")) 

Try this, it will work well in a C # Windows application

0
source

it works for me .....

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

Source: https://habr.com/ru/post/909080/


All Articles