c# listBox DisplayMember

2019-08-07 18:31发布

问题:

c# WinForms.Listbox.

listBox1.DataSource = ds.Tables[0].DefaultView;
listBox1.DisplayMember = "Question";
listBox1.ValueMember = "idQuestion";

//for ValueMember showing...   
textBox2.Text = listBox1.SelectedValue.ToString();

//What I must use for DisplayMember showing?  
textbox3.Text = ??????????

回答1:

That may not be so easy with untyped tables. A Combobox has a Text property, for the listbox:

 textbox3.Text = listBox1.SelectedItem;

Gets you the 'item' but that probably is a DataRowView. You can cast it :
((DataRowViw) SelectedItem).Row[3]



回答2:

This will work with you
textbox3.Text = listBox1.GetItemText(listBox1.SelectedItem);