473,386 Members | 1,673 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,386 software developers and data experts.

Returning ID from Listbox bound to DataTable

I've got a list box bound to a Datatable, like this:

lstProjects.DataSource = m_oProjectSet.DataTable
lstProjects.DisplayMember = "ProjectDesc"
lstProjects.ValueMember = "ProjectID"

I want to pass the ProjectID value (integer) of the selected item in
lstProjects to another function.

I assumed I could reference the integer value of the ProjectID column as
lstProjects.SelectedValue, since I set it's ValueMember property to
"ProjectID". I discovered that SelectedValue returns a DataRowView.

Through trial and error I found that I can get to the integer value with:

lstProjects.DataSource.DefaultView(lstProjects.Sel ectedIndex).Item("ProjectI
D")
or
m_oProjectSet.DataTable.DefaultView(lstProjects.Se lectedIndex).Item("Project
ID")

Seems like a lot of code to do something so simple and so common. (I know
it's one line, but it should be shorter, and more intuitive, given that data
binding should make things like this simple.)

Is there a simpler expression that would give me the integer value?

What is the purpose of the ValueMember property, if not that which I tried
to use it?

Thanks!
Nov 20 '05 #1
6 2217
> Is there a simpler expression that would give me the integer value?

lstProjects.SelectedValue

--
Eduardo A. Morcillo [MS MVP VB]
http://www.mvps.org/emorcillo
Nov 20 '05 #2
This returns a datarowview. How do get the integer value?

"Eduardo A. Morcillo [MS MVP]" <emorcilloATmvps.org> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Is there a simpler expression that would give me the integer value?


lstProjects.SelectedValue

--
Eduardo A. Morcillo [MS MVP VB]
http://www.mvps.org/emorcillo
Nov 20 '05 #3
Cor
Hi Young,

I dont see it not more today but show some more of your code or be sure that
in this piece of code "ProjectID" is exactly right

lstProjects.ValueMember = "ProjectID"

Let us know if this was the error will you?

And otherwise the code with wich you fill your datatable or dataset.

Cor
Nov 20 '05 #4
Thanks again for your response.

My original post is included below. DataTable is a property of type
DataTable of an object m_oProjectSet, which is a private variable in the
form.
It's just a DataTable. By that I mean that it is not part of a DataSet. I'm
pretty sure I've coded it the way you mention below.

I'm sure the DataTable is filled correctly, because the correct rows and
values are there when I reference them directly, and the values display
correctly in the list box. (I'd post the code, but I use seperate data
access components, and I'd have to post three different complete class
listings for you to trace how the table gets filled and past to the business
object.)

I was hoping for a simple way to get the integer value directly from the
list box.

-----------------

I've got a list box bound to a Datatable, like this:

lstProjects.DataSource = m_oProjectSet.DataTable
lstProjects.DisplayMember = "ProjectDesc"
lstProjects.ValueMember = "ProjectID"

I want to pass the ProjectID value (integer) of the selected item in
lstProjects to another function.

I assumed I could reference the integer value of the ProjectID column as
lstProjects.SelectedValue, since I set it's ValueMember property to
"ProjectID". I discovered that SelectedValue returns a DataRowView.

Through trial and error I found that I can get to the integer value with:

lstProjects.DataSource.DefaultView(lstProjects.Sel ectedIndex).Item("ProjectI
D")
or
m_oProjectSet.DataTable.DefaultView(lstProjects.Se lectedIndex).Item("Project
ID")

Seems like a lot of code to do something so simple and so common. (I know
it's one line, but it should be shorter, and more intuitive, given that data
binding should make things like this simple.)

Is there a simpler expression that would give me the integer value?

What is the purpose of the ValueMember property, if not that which I tried
to use it?

Thanks!

"Cor" <no*@non.com> wrote in message
news:OI**************@TK2MSFTNGP12.phx.gbl...
Hi Young,

I dont see it not more today but show some more of your code or be sure that in this piece of code "ProjectID" is exactly right

lstProjects.ValueMember = "ProjectID"

Let us know if this was the error will you?

And otherwise the code with wich you fill your datatable or dataset.

Cor

Nov 20 '05 #5
> I've got a list box bound to a Datatable, like this:

lstProjects.DataSource = m_oProjectSet.DataTable
lstProjects.DisplayMember = "ProjectDesc"
lstProjects.ValueMember = "ProjectID"


Then SelectedValue (NOT SELECTEDITEM!!!) returns the value of the column with the ValueMember name of the selected item.

--
Eduardo A. Morcillo [MS MVP VB]
http://www.mvps.org/emorcillo
Nov 20 '05 #6
Cor
Hi Young,

This is so not my kind of stuff, but this I keep it with the last respond of
me.

I think in that one ore the other way the listbox does not accept your
property from your table as a valuemember
lstProjects.DataSource.DefaultView(lstProjects.Sel ectedIndex).Item("ProjectI D") I think also that here is written (you reference direct to the table)
table.rows(index).item("ProjectId)
m_oProjectSet.DataTable.DefaultView(lstProjects.Se lectedIndex).Item("Project ID")


And that is the same
table.rows(index).item("ProjectId)

So I agree with you that it is strange?

Cor
Nov 20 '05 #7

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

Similar topics

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...
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...
3
by: David L Wright II | last post by:
I have a data bound listbox that I want to select multiple lines then process each line. I bound a two column datatable to the listbox and set the DisplayMember property to the appropriate column...
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...
1
by: Spock | last post by:
Hi. I have a form with a listbox and a few label fields all bound to a dataset. When i navigate the listbox the labels change accordingly. so far everything works good. I made a button to...
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...
1
by: cptkirkh | last post by:
I havea listbox bound to a datatable. When the form loads it thinks the first item is selected. Is there anyway to tell the listbox when it loads not to select the first time automatically?
15
by: Joseph Geretz | last post by:
I'm a bit puzzled by the current recommendation not to send Datasets or Datatables between application tiers. http://support.microsoft.com/kb/306134 ...
2
by: Randy | last post by:
I have two listboxes on a form. The first box displays categories while the second box displays the items belonging to the category selected in the first box. Thus, the second box is essentially...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.