How to get ValueMember value from ComboBox C# Winforms?

前端 未结 3 1949
陌清茗
陌清茗 2021-01-04 18:52

I\'m having some trouble trying to get the ValueMember value I\'ve set. I\'m trying to use a combobox to select a windows forms report. I can get the Name but not RptValue.

3条回答
  •  北海茫月
    2021-01-04 19:09

    You should use the DataSource property. Try this:

    BindingList _comboItems = new BindingList(); 
    _comboItems.Add(new Data { Name = "Select", RptValue = "Select" });
    _comboItems.Add(new Data { Name = "All Food Values", RptValue = "AllFoodValues.rdlc" });
    ...
    comboBox1.DataSource = _comboItems;
    comboBox1.DisplayMember = "Name";
    comboBox1.ValueMember = "RptValue";
    

    And then access the selected value:

    strReport = "ReportViewer." + comboBox1.SelectedValue;
    

提交回复
热议问题