.NET 3.5 列表框选定值 (Winforms)

发布于 2024-08-28 03:10:58 字数 307 浏览 11 评论 0原文

我正在努力从启用了多选并已绑定到数据库表的 Winforms 列表框中获取选定的值(请注意值而不是文本),获取名称(作为 DisplayMember)和 ID(作为 ValueMember) - 我需要所选项目的 ID。

列表框控件具有 SelectedValue 属性,用于获取选定项值之一,但不能获取所有选定项值。

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

请帮忙!谢谢。

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.

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

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

Please help! Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盛装女皇 2024-09-04 03:10:58

尝试将集合中的每个对象转换为所需的类型。例如,如果我的项目属于 Customer 类型,我可以执行以下操作...

var selected = listBox1.SelectedItems;

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

现在您可以从 Customer 获取所需的任何属性。

这只是一个简单的例子,但我相信您可以将这个概念应用于您的问题。

更新(问题更新后表明列表框已绑定到表):

如果您绑定到DataTable,您可以尝试这样的操作(同样,微不足道,但相关):

var selected = listBox1.SelectedItems;

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

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

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.

UPDATE (after question was updated to indicate the Listbox is bound to table):

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];
}
三人与歌 2024-09-04 03:10:58

SelectedItems 就是您想要的。

仅当您设置 DisplayMember 和 ValueMember 时,SelectedItem 和 SelectedValue 才会不同。我认为多选不支持此功能。

您要添加到列表框中的项目是什么类型?

SelectedItems is what you want.

SelectedItem and SelectedValue are only different when you set DisplayMember and ValueMember. I don't think this is supported for Multi-select.

What type of Items are you adding to the listbox?

Saygoodbye 2024-09-04 03:10:58

元素[0] 将是“ValueMember”,元素[1] 将是“DisplayMember”。假设 OP 的“ID”字段是一个整数,请尝试以下操作:

int selectedCount = lstBxOb10Customer.SelectedItems.Count;
int[] selectedIDs = new int[selectedCount];
string[] selectedNames = new string[selectedCount];

for (int selected = 0; selected < selectedCount; selected++)
{
    var itemArray = ( (DataRowView)item ).Row.ItemArray;

    selectedIDs[selected]  = (int)itemArray[0];
    selectedNames[selected]  = (string)itemArray[1];
}

Element [0] will be the "ValueMember", and element [1] will be the "DisplayMember". Assuming the OP's "ID" field is an integer, try the following:

int selectedCount = lstBxOb10Customer.SelectedItems.Count;
int[] selectedIDs = new int[selectedCount];
string[] selectedNames = new string[selectedCount];

for (int selected = 0; selected < selectedCount; selected++)
{
    var itemArray = ( (DataRowView)item ).Row.ItemArray;

    selectedIDs[selected]  = (int)itemArray[0];
    selectedNames[selected]  = (string)itemArray[1];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文