0
votes

I am trying to set my ComboBox's SelectedIndex value and when I do the text that was in the ComboBox gets cleared out. For example: I have a DataGridView and when the user clicks on a row it populates the ComboBox. If the user clicks on another row with the same value, it clears out the ComboBox's value but still keeps the selected index. But if the user clicks on a row with a different value it changes correctly.

I've tried changing how I populate the combo box from setting it's data source to looping through its data and manually setting each value. I've also tried setting the ComboBox1.SelectedIndex = 2 and it still clears out the text.

Here's how I set the SelectedIndex in the Click event of the DataGridView1:

effectiveMonth = DateTime.ParseExact(DataGridView1.SelectedRows(0).Cells.Item("Effective_Month").Value, "MMMM", CultureInfo.InvariantCulture).Month
ComboBox1.SelectedIndex = effectiveMonth - 1

And here is how I fill the ComboBox:

Dim months as New Dictionary(Of Integer, String) FROM {{1, "1-January"}, {2, "February"}, 
{3, "March"}}

ComboBox1.DataSource = months.ToList
ComboBox1.DisplayMember = "Value"
ComboBox1.ValueMemeber = "Key"

What I expect to happen is the value of the ComboBox1 to stay showing when a user clicks on another row with the same value.

1

1 Answers

0
votes

Setting SelectedIndex to -1 would clear the ComboBox leaving it with no selected item. If effectiveMonth is always 0, then you would get the behavior that you described. I cannot be completely sure because not enough code is shown.