Returning ID from Listbox bound to DataTable

  • Thread starter Thread starter Young J. Putt
  • Start date Start date
Y

Young J. Putt

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.SelectedIndex).Item("ProjectI
D")
or
m_oProjectSet.DataTable.DefaultView(lstProjects.SelectedIndex).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!
 
Is there a simpler expression that would give me the integer value?

lstProjects.SelectedValue
 
This returns a datarowview. How do get the integer value?

Eduardo A. Morcillo said:
Is there a simpler expression that would give me the integer value?

lstProjects.SelectedValue
 
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
 
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.SelectedIndex).Item("ProjectI
D")
or
m_oProjectSet.DataTable.DefaultView(lstProjects.SelectedIndex).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!
 
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.
 
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.SelectedIndex).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.SelectedIndex).Item("Project
ID")

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

So I agree with you that it is strange?

Cor
 
Back
Top