简体   繁体   English

如何在C#中获取列表框的选定值

[英]how to get the selected values of Listbox in c#

i had bind the listbox where datasource of listbox is list.how can i get the selected value of selected list item in listbox.my sample code as follows 我已经绑定了列表框,其中列表框的数据源是列表。如何在列表框中获取选定列表项的选定值,我的示例代码如下

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

can anyone solve my problem please. 任何人都可以解决我的问题。

First of all, the Count of a list can never be less than zero. 首先,列表的Count永远不能小于零。 It's always >= 0 . 始终>= 0

Then, when you data-bind your list, the items usually are of type DataRowView (which you should be able to verify by debugging your application). 然后,当您对列表进行数据绑定时,这些项目通常是DataRowView类型的(应该可以通过调试应用程序进行验证)。 If that is right, you should have to cast each selected item to DataRowView and then cast the value of its Row property to the type you expect. 如果正确,则必须将每个选定的项目DataRowViewDataRowView ,然后将其Row属性的值DataRowView为所需的类型。


I just noticed that in the following loop you're not even using the selected items, but always the SelectedValue : 我只是注意到,在以下循环中,您甚至没有使用选定的项,而是始终使用SelectedValue

for (int i = 0; i < lstCompanies.SelectedItems.Count; i++ )
{
    attendanceCalculationDetailsData.CompanyID.Add(int.Parse(lstCompanies.SelectedValue.ToString()));
}

Try changing this to: 尝试将其更改为:

for (int i = 0; i < lstCompanies.SelectedItems.Count; i++ )
{
    attendanceCalculationDetailsData.CompanyID.Add(((<WhatEverClassYouUse>)lstCompanies.SelectedItems[i]).CompanyID);
}

Explanation: If more than one item in the list are selected, the items are added to the SelectedItems collection. 说明:如果选择了列表中的多个项目,则将这些项目添加到SelectedItems集合中。 You can iterate these items. 您可以迭代这些项目。 Each item will be an object of DataRowView (when data bound to a DataTable or DataView ) or a class in a collection. 每个项目将是DataRowView的对象(将数据绑定到DataTableDataView )或集合中的类。

As you didn't tell us the type of objects returned by GetPredefinedDetails , I substituted it with WhatEverClassYouUse . 正如您没有告诉我们GetPredefinedDetails返回的对象类型GetPredefinedDetails ,我将其替换为WhatEverClassYouUse Cast this to the right type. 将此类型转换为正确的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM