How to select listbox item by ValueMember

If I have two items with the same DisplayMember but a different ValueMember and i want to select one of the two items programmatically, how do i do this?

items:

123 -> Peter Pan 234 -> John Doe 345 -> Peter Pan

I can not select the last Peter Pan by doing

Listbox1.FindStringExact("Peter Pan");

because this only returns the first occurence The following also doesn't work, because it only sets the selected item, but doesn't show it in the list:

Listbox1.SelectedItem = dataTable.Rows.Find(345);

Who can help me with this one?

Update:

Found some more info myself, the list is sorted, therefore when I update the DataTable (which is used to fill the list) the list is resorted, and it seems to select the item that was in place of the edited item.

Listbox1.FindStringExact does only work if the DisplayMember is different

-------------Problems Reply------------

You can use the SelectedValue property of your list control:

Listbox1.SelectedValue = 345;

You must assign data via DataSource property of ListBox control, not via Items.Add. After that you can use ValueMember to select items:

listBox1.DataSource = GetPeople();
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Id";

// Now you can use
listbox1.SelectedValue = 345;

UPDATE: Items is a member of ListBox class, but SelectedValue is a ListControl property, which can use only DataSource.

Category:c# Views:0 Time:2011-02-07

Related post

  • WPF select listbox item on right click after scrolling listbox 2010-11-19

    I find some solution how select listbox item on right mouse button click, but any solution does not work after I scroll listbox. It possible select listbox on right mouse button, if I scroll also listbox? Any advance? My code is here: <ListBox Nam

  • Wpf style resource for foreground color of selected listbox item 2009-02-12

    Background: I am creating a custom listbox that has radio buttons on each listbox item, so essentially it will be a RadioButtonList. The control is created entirely in code. As of right now the control renders and behaves correctly and supports 2 ori

  • Vb.net - Select listbox item based on string 2010-01-29

    I'm trying to write a small app which can be used in order to log off Citrix Sessions. Citrix Sessions can be logged off via cmd or a batch file using this method: @echo off echo "Enter username" set /p UserName= echo 001 query user %username% /serve

  • How to use javascript or jQuery to quickly select a preset of multi-select listbox items? 2010-02-01

    So say my page has a list of indexes '1,3,4,5,9,12' and a multi-select listbox with 12 items in it. What's a fast way to use javascript to tell the listbox to multi-select all items at those indexes? How would this be done using jQuery? So for exampl

  • Scrolling to the Previously Selected ListBox item 2011-03-05

    I am having trouble setting the Selected Item of a Listbox I am populating and adding to the LayoutRoot's children in code. I am creating the ListBox over when going back to the page, so I am saving a variable which will tell me what the selected ite

  • Click on ListBox with DataTemplate, select ListBox Item 2011-05-13

    When you click on a control inside the ListBox (its DataTemplate), I want to know which item that is so that I can select that item. I have a WPF application with a ListBox. Using a DataTemplate, I have a TextBlock and a Textbox inside it. <ListBo

  • unable to select listbox item through data template in WPF 2011-09-02

    I am unable to select desired item from the listbox (when i click on any item in that listbox the items more than one are getting selected but not the one on which i have clicked). Also the background color of the selected items are getting changed t

  • How to set an Image source to a blank Image control at runtime from selected ListBox Item? 2011-12-21

    PROBLEM: I have a blank Image control named "Image1". Now, I want to supply a Source to that Image at runtime base on the selected item on my ListBox (ListBoxSource). How will I do that in "ListBoxSource_SelectionChanged(...)" event? private void Lis

  • C# How to select ListBox item with a RightClick? 2012-02-10

    I have tried a lot of methods for this and done hours of research, but it just never seems to work for me. This is my current code, and I don't know why it shouldn't work. private void listBox1_MouseDown(object sender, MouseEventArgs e) { listBox1.Se

  • Windows Phone 7: Highlight Selected Listbox item 2010-08-12

    I have the following XAML (simple list box with custom DataTemplate). I'm trying to figure out how to highlight the selected item (maybe background colour change). I figure I need to do something with Styles in Expression Blend but I'm not quite sure

  • Drag and Select ListBox Items? 2010-12-08

    how to select items in listbox, if start dragging from one item to the end of the dragging? --------------Solutions------------- I found this approach on a question on msdn but I can't find it again so I can't link it.. Anyway, this works pretty good

  • Get name of selected listbox item (WPF C#)? 2011-04-21

    all. I have an app that scans a picture folder and displays the images along with their names in a listbox. Each image and image name (displayed in a textblock next to the image) is stored in a horizontal stackpanel inside the listbox. I've been tryi

  • Select ListBox item only on Click or Enter key in Silverlight 2011-06-17

    When I populate a ListBox with RIA Services, an item is automatically selected. This triggers the SelectionChanged event. If I move the selection up or down with the arrow keys, the event also gets triggered. I don't want this. I want the user to pre

  • How can I retrieve multiple selected listbox items and cast them to objects? 2011-07-07

    Similar question: .NET 3.5 Listbox Selected Values (Winforms) I have a listbox populated using data binding to a collection of objects: lstbDataFields.DisplayMember = "HumanReadable"; lstbDataFields.ValueMember = "DatabaseName"; lstbDataFields.DataSo

  • Select ListBox item on rightclick in Word VBA 2012-02-01

    I'm developping a project in Word 2003 with VBA. I have a multiselect ListBox with some entries (dates). On rightclick I'd like to have an InputBox popping up where the user can change the selected date. This works well, as long a specific item is al

  • Get selected ListBox item 2012-02-13

    I'm toying with this for 1 day already and I'm about the give it up. Basically the point is I got a ListBox containing custom objects (Products) wich are coming from a database. Filling and sorting works but getting the values out of the selected ite

  • How do you get the selected listBox item as a String? 2013-03-22

    I have been trying to build a small app that takes the selected item from a listBox and appends it to the filename of a file that will be uploaded. I can display everything correctly, and the file gets uploaded, but the value that should be appended

  • How to programatically select an item in a data bound ListBox control 2012-01-02

    I have a custom styled ListBox: <phone:PhoneApplicationPage.Resources> <Style x:Key="LayoutsListItemStyle" TargetType="ListBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem">

  • Setting focus on a ListBox item breaks keyboard navigation 2010-02-08

    After selecting ListBox item programmatically it is needed to press down\up key two times to move the selection. Any suggestions? View: <ListBox Name="lbActions" Canvas.Left="10" Canvas.Top="10" Width="260" Height="180"> <ListBoxItem Name="O

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.107 (s). 11 q(s)