如何从组合框C#Winforms中获取ValueMember值?
我在尝试获取已设置的ValueMember值时遇到一些问题。我正在尝试使用组合框选择windows窗体报表。我可以得到名称,但不能得到值。这是我的密码:如何从组合框C#Winforms中获取ValueMember值?,c#,winforms,report,C#,Winforms,Report,我在尝试获取已设置的ValueMember值时遇到一些问题。我正在尝试使用组合框选择windows窗体报表。我可以得到名称,但不能得到值。这是我的密码: private class Data { public string Name { get; set; } public string RptValue { get; set; } } private void BaseForm_Load(object sender, E
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();
}
}
你应该使用这个属性。试试这个:
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";
这对我很有用:
combobox.valuemember="id"
combobox.displaymember="name"
combobox.datasource=dt
好的,现在我在报告本身上得到了一个不同的错误。它表示“在本地报表处理过程中发生错误。尚未指定“reportname”的报表定义。对象引用未设置为对象的实例。“有什么想法吗?@bbcompent1在没有看到一些代码的情况下无法肯定地说什么。”我猜它与
ReportPath
有关(但我不确定)。我将建议发布一个关于此特定问题的新问题。好的,以下是该问题的链接:
String s;
s=comboBox1.SelectedValue.tostring()
combobox.valuemember="id"
combobox.displaymember="name"
combobox.datasource=dt