I have a ListBox and a TextBox on my form. Upon ListBox's SelectedIndexChanged
event I want to show what is currently selected in the ListBox. The ListBox is bound to a DataSource, the type of which is unknown at design-time. I do know the value of DisplayMember
(which the ListBox apparently uses to display each item's text). How do I get the selected item's Text? Does it need Reflection, or is there a direct way?
If the selected item is a datarowview, then you can use the displaymember to get the displayValue with:
System.Data.DataRowView drv = (DataRowView)listBox1.SelectedItem;
string displayValue = Convert.ToString(drv[listBox1.DisplayMember]);
There's also the getItemText function:
listBox1.GetItemText(listBox1.SelectedItem);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.