我有綁定列表框,其中列表框的數據源是list.how可以我得到選擇的列表項的所選擇的值在listbox.my示例代碼如下如何獲得列表框的選定值在c#
pdfList = attendanceDetailsPresenter.GetPredefinedDetails();
this.lstCompanies.DataSource = pdfList;
this.lstCompanies.DisplayMember = "CompanyName";
this.lstCompanies.ValueMember = "CompID";
this.lstDepartments.BindingContext = new BindingContext();
this.lstDepartments.DataSource = pdfList;
this.lstDepartments.DisplayMember = "DepartmentName";
this.lstDepartments.ValueMember = "DeptID";
if (lstCompanies.SelectedItems.Count < 0)
{
MessageBox.Show("Please Select Any one Company");
return attendanceCalculationDetailsDataList;
}
else
{
for (int i = 0; i < lstCompanies.SelectedItems.Count; i++)
{
attendanceCalculationDetailsData.CompanyID.Add(int.Parse(lstCompanies.SelectedValue.ToString()));
}
}
任何人都可以解決我的問題。
爲什麼不循環'foreach(var selectedItem in lstCompanies.SelectedItems)'? –
另一個可能的錯誤:我不認爲這將是真實的:「lstCompanies.SelectedItems.Count <0」。我相信伯爵不會是負面的。 – neontapir
您是否嘗試過在for循環中設置斷點以查看發生了什麼? – emd