【问题标题】:Combobox value getting cleared out after setting SelectedIndex设置 SelectedIndex 后,组合框值被清除
【发布时间】:2019-07-01 21:11:01
【问题描述】:

我正在尝试设置我的 ComboBox 的 SelectedIndex 值,当我这样做时,ComboBox 中的文本会被清除。例如:我有一个 DataGridView,当用户单击一行时,它会填充 ComboBox。如果用户单击具有相同值的另一行,它将清除 ComboBox 的值,但仍保留选定的索引。但是,如果用户单击具有不同值的行,它会正确更改。

我尝试将组合框的填充方式从设置它的数据源更改为循环其数据并手动设置每个值。我也试过设置 ComboBox1.SelectedIndex = 2 ,它仍然清除文本。

以下是我在 DataGridView1 的 Click 事件中设置 SelectedIndex 的方法:

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

这是我如何填充 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"

我希望 ComboBox1 的值在用户单击具有相同值的另一行时保持显示。

【问题讨论】:

    标签: .net vb.net combobox


    【解决方案1】:

    SelectedIndex 设置为-1 将清除组合框,使其没有选定项。如果effectiveMonth 始终为0,那么您将得到您所描述的行为。我不能完全确定,因为没有显示足够的代码。

    【讨论】:

    • 知道为什么这是预期的行为吗?在再次设置之前,我会尝试将其设置为 -1。
    • 还要确保effectiveMonth 大于0
    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多