473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

ListBox.DataSource = DataTable... How to Set Selected?

Ok, I have a DataTable:

ID Name
-- ------------
1 John
2 Jacob
3 Jingleheimer
4 Schmidt
5 John
I have set my DisplayMember of my listbox to "Name"; my valueMember to "ID"
and my datasource to myDataTable. So far so good. It shows the list, and
all is well.

What I need to do is be able to set the Selected Item via code. I have done
everything I can think of here, and nothing seems to work. I have tried:

myListBox.SelectedValue = 5 (this is the obvious one that should have worked)
myListBox.SelectedItem = mySelectedRow (a valid reference to the selected
rowview)
myListbox.SetSelected("John", True) This sets the first John, not the
second one.
myListbox.FindString ("John") same problem as above.

I've done this before... What simple thing am I missing here???
--
--Zorpie
Nov 21 '05 #1
2 4560
Hi,

myListBox.SelectedIndex = 4

http://msdn.microsoft.com/library/de...indextopic.asp
Ken
----------------------
"Zorpiedoman" <no*********@beatles.com> wrote in message
news:CB**********************************@microsof t.com...
Ok, I have a DataTable:

ID Name
-- ------------
1 John
2 Jacob
3 Jingleheimer
4 Schmidt
5 John
I have set my DisplayMember of my listbox to "Name"; my valueMember to "ID"
and my datasource to myDataTable. So far so good. It shows the list, and
all is well.

What I need to do is be able to set the Selected Item via code. I have done
everything I can think of here, and nothing seems to work. I have tried:

myListBox.SelectedValue = 5 (this is the obvious one that should have
worked)
myListBox.SelectedItem = mySelectedRow (a valid reference to the selected
rowview)
myListbox.SetSelected("John", True) This sets the first John, not the
second one.
myListbox.FindString ("John") same problem as above.

I've done this before... What simple thing am I missing here???
--
--Zorpie
Nov 21 '05 #2
Fine solution, provided you know the index is 4...

However, I only know the ID I want is 5...

-Zorpy
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gordon | last post by:
Hi; I am a novice asp.net developer who would like to create a dataset-table and then use the datatable.select cmd. to look up a value passed from the web calender control. The value(s) would...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
5
by: Lie | last post by:
Hi all, I have problem in getting selectedindex of multiple listbox selection in a datagrid. I have a listbox with multiple selection mode inside datagrid. In Edit mode, I need to get back all...
1
by: JMann101 | last post by:
I am writing a ASP.NET(VB) application and am having some trouble. I have a datagrid which list users and when an admin clicks "edit" a defined column becomes visible and a dynamic listbox control...
6
by: Sparky Arbuckle | last post by:
The code below works to an extent. The problem with it is that no matter which item is selected, the first one in the list is always the item that gets added/removed. How can I alleviate this...
0
by: amber | last post by:
Hello, I'm having problems with a listbox... I have a listbox that is populated when a user presses a button (retrieve). There is a textbox on the form, and if the textbox is left blank, and...
28
by: cjobes | last post by:
Hi all, I need to populate a listbox from a table column. The column has multiple entries of usernames and I need to pull a unique set of usernames. The table is part of an untyped Dataset that...
5
by: Dave | last post by:
Hi All, I have a windows form that contains 2 listboxes and 2 buttons. The listbox on the right is populated by a database routine (This is the easy part). The listbox on the left is populated...
7
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've looked for over a week now and can't find anything but...
7
by: Zorpiedoman | last post by:
I'm confused. I have a listbox. The DataSource is a DataTable based on a SQL Select Statement that includes a where clause. For example, "Select ID, FirstName, LastName From Employees where...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.