How to get ValueMember value from ComboBox C# Winforms?

88,979

Solution 1

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;

Solution 2

  String s;
  s=comboBox1.SelectedValue.toString()
Share:
88,979
bbcompent1
Author by

bbcompent1

I am an Application Security Specialist for a very large health organization. I have over 30 years of professional experience starting out as a technician leading up to programming and everything in between.

Updated on August 08, 2021

Comments

  • bbcompent1
    bbcompent1 almost 3 years

    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. Here's my code:

            private class Data
        {
            public string Name { get; set; }
            public string RptValue { get; set; }
        }
    
        private void BaseForm_Load(object sender, EventArgs e)
        {
            this.rvDoctorReportViewer.RefreshReport();
            comboBox1.Items.Add(new Data { Name="Select", RptValue="Select"});
            comboBox1.Items.Add(new Data { Name = "All Food Values", RptValue = "AllFoodValues.rdlc" });
            comboBox1.Items.Add(new Data { Name = "All Readings", RptValue = "AllReadings.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Avg Food Values by Date", RptValue = "AvgFoodValuesByDate.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Avg Food Values by Meal", RptValue = "AvgFoodValuesByMeal.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Avg Readings by Date", RptValue = "AvgReadingsByDate.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Avg Readings by Time", RptValue = "AvgReadingsByTime.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Avg Readings by Event", RptValue = "AvgReadingsByEvent.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Blood Pressure Chart", RptValue = "BPChart.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Blood Pressure Report", RptValue = "BPReport.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Detail Food Values by Meal", RptValue = "DetailFoodValuesByMeal.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Doctor Detail Report", RptValue = "DoctorDetailReport.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Food Chart", RptValue = "FoodChart.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Pumper Detail Report", RptValue = "PumperDetailReport.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Reading Charts", RptValue = "ReadingCharts.rdlc" });
            comboBox1.Items.Add(new Data { Name = "Total Daily Food Intake", RptValue = "TotalIntakeDailyFood.rdlc" });
            comboBox1.DisplayMember = "Name"; // This works fine
            comboBox1.ValueMember = "RptValue"; // This is the problem. It renders as RptValue instead of the value
            comboBox1.SelectedIndex = 0;
        }
    
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex > 0)
            {
                string strReport;
                strReport = "ReportViewer." + comboBox1.ValueMember.ToString();
                rvDoctorReportViewer.Reset();
                rvDoctorReportViewer.LocalReport.ReportEmbeddedResource = strReport;
                this.rvDoctorReportViewer.RefreshReport();
            }
        }
    
  • bbcompent1
    bbcompent1 about 11 years
    Ok, now I'm getting a different error on the report itself. It says "An error occurred during local report processing. The report definition for "reportname" has not been specified. Object reference not set to an instance of an object." Any ideas?
  • MAV
    MAV about 11 years
    @bbcompent1 Can't say anything for sure without seeing some code. :) I would guess it has something to do with ReportPath (but I'm not sure). I will recommend posting a new question about this specific issue.
  • bbcompent1
    bbcompent1 about 11 years
    Ok, here is the link to that question: stackoverflow.com/questions/16024549/…