How to get ValueMember value from ComboBox C# Winforms?

前端 未结 3 1950
陌清茗
陌清茗 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<Data> _comboItems = new BindingList<Data>(); 
    _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;
    
    0 讨论(0)
  • 2021-01-04 19:15
      String s;
      s=comboBox1.SelectedValue.tostring()
    
    0 讨论(0)
  • 2021-01-04 19:20

    This worked for me:

        combobox.valuemember="id"
        combobox.displaymember="name"
        combobox.datasource=dt
    
    0 讨论(0)
提交回复
热议问题