Referencing ID column in 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, assuming that
databinding 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 for doing what I
tried to do?

Thanks!
 
Sorry for the multipost. My newsreader crashed while I was submitting, and
it didn't appear as though the message posted. Thanks for your response.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top