I have a listBox, something like this:
ID ClientName
Now, I know the ID of the client , how do i get the DisplayMember of it ?
转换为DataRowView
并转换为按索引选择客户端名称列,即1。
String ClientName = (((DataRowView) listBox1.SelectedItem).Row[1]).ToString();
You could try this code. the example is written if the DataSource
of the Listbox
is DataTable
, but if it is something else, try to see of what type is the object
in Items property of the ListBox
.
(Note that I don't like using break
statement but this was the quickest way)
string str = string.Empty;
foreach (DataRowView drv in listBox1.Items)
{
int lvID = int.Parse(drv.Row[listBox1.ValueMember].ToString());
if (lvID == 5)
{
str = drv.Row[listBox1.DisplayMember].ToString();
break;
}
}
var t = from list in USStates where te.ShortName == textBox1.Text select te.LongName.ToString();
foreach(String st in t)
{
MessageBox.Show(st);
}
where USStates is Generic List and TextBox1.text contains the value of the id.Considering your listbox uses Generic list as DataSource.
There is a much easier solution. Especially if you are building the DataSource, populating it, then releasing the source. Use the Items list to create a DataRowView of the item currently selected. Then just pull the right index. 0 will give you the "value member" and 1 will give you the "display member".
DataRowView thisLine = cbTrackerFormList.Items[cbTrackerFormList.SelectedIndex] as DataRowView;
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.