【发布时间】:2015-07-19 06:31:24
【问题描述】:
第一次发帖,希望我没有重复已经提出的问题,我已经进行了几次搜索,并且多次重写了我的代码,但无济于事。我的问题来自多个角度。关于我的程序的一些背景信息。
我实际上是在创建一个客户数据库,该数据库将 Customer 类型的对象存储在列表 CustomerEntry 中,并将 InspectionReport 类型的对象存储在列表 ReportList 中。然后,我使用 ListView 显示 CustomerList Items 的详细视图以供选择。从此用户可以添加更多客户、查看选定客户、删除客户、添加报告和查看报告(一旦添加)。
我的问题是,一旦在 ComboBox 中选择了一份报告并选择查看它,它就会在列表中查看错误的项目。
我的代码的问题区域如下:
private void ViewReportButton_Click(object sender, EventArgs e)
{
int selIndex = CustomerListView.SelectedIndices[0];
int selIndex2 = AvailableReportsDropDownBox.SelectedIndex;
tabControl.SelectedTab = tabInspReport;
populateViewReportTabOutputControls(selIndex, selIndex2);
activateViewReportTabOutputControls();
}
public void populateViewReportTabOutputControls(int selIndex2)
{
ViewCustNameLabel.Text = ReportList[selIndex2].getCustomerName();
ViewDateLabel.Text = Convert.ToString(ReportList[selIndex2].getInspectionDate());
ViewAddressLabel.Text = ReportList[selIndex2].getCustomerAddress();
AeratorStatusBox.Text = ReportList[selIndex2].getAeratorStatus();
FilterStatusBox.Text = ReportList[selIndex2].getFilterStatus();
HLAFloatStatusBox.Text = ReportList[selIndex2].getHLAFloatStatus();
OnOffFloatStatusBox.Text = ReportList[selIndex2].getOnOffFloatStatus();
SprayHeadStatusBox.Text = ReportList[selIndex2].getSprayHeadStatus();
SludgeLevelOutputBox.Text = Convert.ToString(ReportList[selIndex2].getSludgeLevel());
InspectorsNameOutputBox.Text = ReportList[selIndex2].getInpsectorName();
}
我想知道的是;有没有更好的方法来显示 ComboBox 中的选定项目,该项目将链接到特定的非数据绑定列表项。
例如,我选择了客户[1],报告列表中有 10 个报告,其中 4 个属于所述客户,但是当我选择组合框中的第一个报告时,而不是给我ReportList[0] 中的项目,它告诉我 ReportList[4],这是所选客户的第一份报告。
提前致谢。
【问题讨论】:
-
当你调用这个函数 populateViewReportTabOutputControls 你传递了两个参数并且声明函数有一个参数这意味着你传递了错误的索引
-
是的,方法调用上的 selIndex 不应该存在,它不重要。这是因为尝试做其他事情。但我面临的问题是,使用 selIndex2 作为我的索引值,因为它与组合框中项目的索引有关,而不是它在列表中的项目的索引。我想要的可能是将组合框中项目的索引与它们在 ReportList 中的相应索引联系起来的方法。
-
也许你应该只调试你的代码,遍历每一行并检查它的作用(尤其是它处理的值)。通过这种方式,您将找出索引不匹配的原因。
-
欣赏小费,这始终是我做的第一件事。而且我知道它做错了什么以及为什么。它正在寻找一个合适的、精简编码的工作,就像我在下面发布的那样,这就是问题所在。
标签: c# list class listview combobox