从数据库馈送的ListBox获取SelectedItems [英] Get SelectedItems from database fed ListBox

查看:164
本文介绍了从数据库馈送的ListBox获取SelectedItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从与数据库绑定的列表框中获取选定项的值.但是,如果我尝试

I need to get values from selected items from listbox which is binding from database. But if I try

listBoxAtribute.SelectedItems[0].ToString()

它返回System.Data.DataRowView

有什么方法可以将数据从DataRowView转换为string?

Is there any way to convert Data from DataRowView to string?

我的想法如下:

for(int i = 0; i < listBoxAtribute.SelectedItems.Count; i++)
{
  MessageBox.Show(listBoxAtribute.SelectedItems[i].Tostring);
}

非常感谢您的回复.

推荐答案

假设您看到的是可见的数据库驱动条目中的单个特定列,这将表明您具有DisplayMember属性列表框集.假设您在任何地方都使用EditValue,也可能使用ValueMember.我会按照这些思路写一些东西.

Under the assumption that you are seeing, visibly that is, a single specific column from said database driven entries, that would indicate that you have the DisplayMember property of the listbox set. Possibly the ValueMember as well, assuming you are using EditValue anywhere. I would write something along these lines.

((DataRowView)listBoxAtribute.SelectedItems[0])[listBoxAtribute.DisplayMember].ToString();

这样,您就可以准确地获得用户在屏幕上看到的内容.现在,如果您想要的数据与屏幕上显示的数据不同,则需要使用user968441的方法并对列名进行硬编码.但这也相对容易.

That way you get exactly what the user would see on the screen. Now, if you want a different piece of data than the one shown on the screen, you would need to use user968441's approach and hard code the column name. But that's also relatively easy.

这篇关于从数据库馈送的ListBox获取SelectedItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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