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

后端 未结 2 1422
梦谈多话
梦谈多话 2020-12-22 10:07

I have listbox1 - its datasource is a column (productname).

so i have in the listbox a MultiSelection option.

and im trying to make a Mess

相关标签:
2条回答
  • 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);
    
    0 讨论(0)
  • 2020-12-22 11:02

    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.

    0 讨论(0)
提交回复
热议问题