使用selectedvalues从列表框中存储数据 [英] storing data from listbox using selectedvalus

查看:59
本文介绍了使用selectedvalues从列表框中存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表框,它从sql表中提取数据.我将其选择模式设置为multiextended.

我将每个项目的显示成员设置为sql表的Name字段,并将每个项目的值成员设置为sql表的ID字段.

当我从列表中选择多个项目时,如何获得多个值?我可以使用listboxname.Text来获取一项的文本,但我不知道如何一次获取所有已选择的每个值.

I have a list box that is pulling it''s data from a sql table. I have its selection mode set to multiextended.

I have the display member for each item set to the Name field from the sql table and the value member for each item set to the ID field from the sql table.

When I select multiple items from the list, how do I get the multiple values? I am able to get the text of one item by using the listboxname.Text but I don''t know how to get each value that has been selected all at once.

推荐答案

尝试:
foreach (object o in myListBox.SelectedItems)
   {
   ...
   }


您必须遍历所有项并检查是否已选中,或者仅遍历所选项并获取值.

试试类似的东西:
You have to loop through all items and check if that is selected OR just loop through the selected items and get the value.

Try something like:
// better approach of looping through all selected items only
String stringItem;
foreach(Object selecteditem in listBox1.SelectedItems)
{
  stringItem = selecteditem as String;
  MessageBox.Show(stringItem);
}



参考: MSDN:ListBox.SelectedItems属性 [ ^ ]



Refer: MSDN: ListBox.SelectedItems Property [^]


这篇关于使用selectedvalues从列表框中存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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