.NET 3.5列表框选定值(的WinForms) [英] .NET 3.5 Listbox Selected Values (Winforms)

查看:144
本文介绍了.NET 3.5列表框选定值(的WinForms)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我作战,以获得选择的值(请注意VALUES不是文本)从一个WinForms列表框有多种选择启用,并已绑定到数据库表中获取名称(如的DisplayMember)和身份证(如ValueMember) - 我需要所选项目的ID。

I am BATTLING to get the selected values (please note VALUES not TEXT) from a Winforms Listbox that has multi-select enabled and has been bound to a database table getting the Name (as DisplayMember) and ID (as ValueMember) - I need the ID of the selected items.

ListBox控件具有的SelectedValue 属性来获得一个选定的项目值,但不适用于所有选定项的值。

The listbox control has properties for SelectedValue to get one of the selected items values, but not for all selected items values.

SelectedItems 属性返回一个 Listbox.SelectedObjectCollection 从中我似乎无法提取的项的值。

The SelectedItems property returns a Listbox.SelectedObjectCollection from which I cannot seem to extract the VALUES of the items.

请帮忙!谢谢你。

推荐答案

尝试集合中的每个对象铸造所需的键入。例如,如果我的项目是类型客户,我可以做这样的事情...

Try casting each object in the collection to the desired type. For example, if my items are of type Customer, I could do something like this...

var selected = listBox1.SelectedItems;

foreach ( var item in selected )
{
    var singleCustomer = (Customer)item;
}

现在,你可以选择你想要从客户得到任何财产

Now you can get any property you want from the Customer.

这只是一个简单的例子,但我敢肯定,你可以将概念应用到你的问题。

This is just a trivial example, but I'm sure you can apply the concept to your problem.

更新(后问题被更新,以指示列表框绑定到表):

如果你绑定到数据表,你可以尝试这样的事情(同样,微不足道,但相应和):

If you're bound to a DataTable, you could try something like this (again, trivial but relevent):

var selected = listBox1.SelectedItems;

foreach ( var item in selected )
{
    var itemArray = ( (DataRowView)item ).Row.ItemArray;

    var name = itemArray[0];
    var id = itemArray[1];
}

这篇关于.NET 3.5列表框选定值(的WinForms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆