listbox selected item give me “ System.Data.DataRowView” , C# winforms

点点圈 提交于 2019-11-29 17:32:59
Chris

How do you populate the listbox (ie what is exactly the datasource)?

From your comment I would say a DataView (and wich contains DataRowView...)

So you just need to cast the SelectedItem into DataRowView in order to get a value from this DataRowView:

foreach (object selectedItem in listBox1.SelectedItems)
{
     DataRowView dr = (DataRowView)selectedItem;
     String result = dr["productname"].ToString;
     MessageBox.Show(result + Environment.NewLine);
}

The VB.Net developers that could fall on this post could also be interested in this.

Try to change with this

ListBoxItem lbi ;
String myStr ;

for (int i =0; i <= listbox1.selecteditems.count-1 ; i++)
    {
        lbi = (ListBoxItem)(listBox1.ItemContainerGenerator.ContainerFromIndex(i)); 
        myStr += lbi + Environment.NewLine);            
    }

 MessageBox.Show(myStr);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!