ListBox

  • Thread starter Thread starter Doug Bell
  • Start date Start date
D

Doug Bell

Hi,
I thought that this would be a simple exercise but I am struggling to get a
result.
I need a listbox that will display a list of items from a DataTable,
dtUnits. The displayed list comes from Column [UnitsProdDescn]. Selecting an
item in this list needs to return a value from Column [UnitsConvn].

Data in dtUnits is
UnitsProdDescn UnitsConvn
Carton 3.2
Case*** 0

So in the Form Load Sub I have:
_____________________________________________
stDefaultUnits= "Carton"
dblUnitsConv = 1.0
With ListProdUnits
.DataSource = dtUnits
.DisplayMember = "UnitsProdDescn"
.ValueMember = "UnitsConvn"
.SelectedValue = stDefaultUnit

End With
_____________________________________

And in the ListProdUnits_SelectedValueChanged Event, I have:
_______________________________________________________________
Dim dblVal as Double = Val(ListProdUnits.SelectedValue.ToString)
If dblVal > 0 then
dblUnitsConv = dblVal
LabelProdUnits.Text = ListProdUnits.SelectedItem.ToString

End If
________________________________________________________

The variable dblUnitsConv is being set correctly to 3.2 but
LabelProdUnits.Text
shows "System.Data.DataRowView" instead of "Carton"

Also the list shows Case*** as the selected row which should be Carton.

Can someone tell me what I am doing wrong?

Doug
 
Doug,

Mostly are this kind of errors by a misspelling of the datacolumnname

Are you sure that this is right,
..ValueMember = "UnitsConvn"
(It has real to be case sensitive right).

Not that it can be the error, however why don't you use not as normally the
selected index change event?

I hope this helps.

Cor
 
Hi,

Doug Bell said:
Hi,
I thought that this would be a simple exercise but I am struggling to get
a
result.
I need a listbox that will display a list of items from a DataTable,
dtUnits. The displayed list comes from Column [UnitsProdDescn]. Selecting
an
item in this list needs to return a value from Column [UnitsConvn].

Data in dtUnits is
UnitsProdDescn UnitsConvn
Carton 3.2
Case*** 0

So in the Form Load Sub I have:
_____________________________________________
stDefaultUnits= "Carton"
dblUnitsConv = 1.0
With ListProdUnits
.DataSource = dtUnits
.DisplayMember = "UnitsProdDescn"
.ValueMember = "UnitsConvn"
.SelectedValue = stDefaultUnit
End With

stDefaultUnit ( "Carton" ) is displaytext not a value, so you can't assign
that to SelectedValue. You can assign it to the .Text property which will
find and select the item with that text:

ListProdUnits.Text = stDefaultUnit
_____________________________________

And in the ListProdUnits_SelectedValueChanged Event, I have:
_______________________________________________________________
Dim dblVal as Double = Val(ListProdUnits.SelectedValue.ToString)
If dblVal > 0 then
dblUnitsConv = dblVal
LabelProdUnits.Text = ListProdUnits.SelectedItem.ToString

If the ListBox is databound then SelectedItem returns the current
DataRowView, you can get your text from it using the colname:
LabelProdUnits.Text = ListProdUnits.SelectedItem("UnitsProdDescn")

You can also use ListBox.Text since it returns the displaytext of the first
selected item:
LabelProdUnits.Text = ListProdUnits.Text


--- Or you can databind the label the same way as the listbox, then it will
show the text of the selected item without the need to handle any events:
LabelProdUnits.DataBindings.Add( "Text", dtUnits, "UnitsProdDescn" )


HTH,
Greetings
 
Cor,
Thanks, the name is spelt correctly. When I misspell it (eg wrong case), I
get a different error I get a different error, to do with the member not
being able to be set.

Are you indicating that the coding method looks correct?

Doug
 
Hi Bart,

Thank you.
Setting the ListBox.Text value solved one problem
and setting the TextBox.Text = ListBox.Text solved the other.

I was surprised that the Text property is not displayed in the intelisense
list.

Doug


Bart Mermuys said:
Hi,

Doug Bell said:
Hi,
I thought that this would be a simple exercise but I am struggling to get
a
result.
I need a listbox that will display a list of items from a DataTable,
dtUnits. The displayed list comes from Column [UnitsProdDescn]. Selecting
an
item in this list needs to return a value from Column [UnitsConvn].

Data in dtUnits is
UnitsProdDescn UnitsConvn
Carton 3.2
Case*** 0

So in the Form Load Sub I have:
_____________________________________________
stDefaultUnits= "Carton"
dblUnitsConv = 1.0
With ListProdUnits
.DataSource = dtUnits
.DisplayMember = "UnitsProdDescn"
.ValueMember = "UnitsConvn"
.SelectedValue = stDefaultUnit
End With

stDefaultUnit ( "Carton" ) is displaytext not a value, so you can't assign
that to SelectedValue. You can assign it to the .Text property which will
find and select the item with that text:

ListProdUnits.Text = stDefaultUnit
_____________________________________

And in the ListProdUnits_SelectedValueChanged Event, I have:
_______________________________________________________________
Dim dblVal as Double = Val(ListProdUnits.SelectedValue.ToString)
If dblVal > 0 then
dblUnitsConv = dblVal
LabelProdUnits.Text = ListProdUnits.SelectedItem.ToString

If the ListBox is databound then SelectedItem returns the current
DataRowView, you can get your text from it using the colname:
LabelProdUnits.Text = ListProdUnits.SelectedItem("UnitsProdDescn")

You can also use ListBox.Text since it returns the displaytext of the first
selected item:
LabelProdUnits.Text = ListProdUnits.Text


--- Or you can databind the label the same way as the listbox, then it will
show the text of the selected item without the need to handle any events:
LabelProdUnits.DataBindings.Add( "Text", dtUnits, "UnitsProdDescn" )


HTH,
Greetings

End If
________________________________________________________

The variable dblUnitsConv is being set correctly to 3.2 but
LabelProdUnits.Text
shows "System.Data.DataRowView" instead of "Carton"

Also the list shows Case*** as the selected row which should be Carton.

Can someone tell me what I am doing wrong?

Doug
 

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