如何从ComboBox C#Winforms获得ValueMember值?

时间:2013-04-13 00:41:05

标签: c# winforms report

我在尝试获取我设置的ValueMember值时遇到了一些麻烦。我正在尝试使用组合框来选择Windows窗体报告。我可以得到名字而不是RptValue。这是我的代码:

        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();
        }
    }

3 个答案:

答案 0 :(得分:16)

您应该使用DataSource属性。试试这个:

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";

然后访问所选值:

strReport = "ReportViewer." + comboBox1.SelectedValue;

答案 1 :(得分:7)

  String s;
  s=comboBox1.SelectedValue.tostring()

答案 2 :(得分:0)

这对我有用:

<div class="Aligner">

  <div class=" Aligner-item Aligner-item--top image">Image</div>
  <div class="item">
  <div class="Aligner-item center">Legend<br/>
  width variable height<br/>
  the middle of the legend need to be exactly on the bottom of the image, regardless of the height of the legend and image</div>
  <div class="Aligner-item Aligner-item--bottom following">The following text should follow immediatly the legend,regardless of the height of the legend or the image</div>
  </div>
</div>