By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,952 Members | 1,091 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,952 IT Pros & Developers. It's quick & easy.

Returning ID from Listbox bound to DataTable

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
> 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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.