ListBox Control

  • Thread starter Thread starter straydawg
  • Start date Start date
S

straydawg

Creating listbox for Role and initially populating with loop

myDataTable = myDataSet.Tables(1)
lbRole.Items.Clear()
Dim dataRow_t As DataRow
For Each dataRow_t In myDataTable.Rows
lbRole.Items.Add(dataRow_t("Role_Desc"))
Next dataRow_t
lbRole.SelectedIndex = -1

Thought I would try using binding

lbRole.Items.Clear()
lbRole.DisplayMember = "role_desc"
lbRole.ValueMember = "role_id"
lbRole.DataSource = myDataSet.Tables(1)
lbRole.SelectedIndex = -1

When I use looping method, I am able to set lbRole.SelectedIndex = -1
so no value is selected for the listbox. When I try the same method for
the binding, I still get a value selected.

I would like to bind, but need to be able allow input with no role
selected.

Thanks in advance...
 
Dawg,

Does setting it twice have the desired effect? For example:

lbRole.SelectedIndex = -1
lbRole.SelectedIndex = -1

Kerry Moorman
 
Thanks Kerry...

Not really. I see the the value for the lbRole.SelectedIndex is -1, but
when looking at the listbox on the form, there still is a value
selected. ??

D
 
Creating listbox for Role and initially populating with loop

myDataTable = myDataSet.Tables(1)
lbRole.Items.Clear()
Dim dataRow_t As DataRow
For Each dataRow_t In myDataTable.Rows
lbRole.Items.Add(dataRow_t("Role_Desc"))
Next dataRow_t
lbRole.SelectedIndex = -1

Thought I would try using binding

lbRole.Items.Clear()
lbRole.DisplayMember = "role_desc"
lbRole.ValueMember = "role_id"
lbRole.DataSource = myDataSet.Tables(1)
lbRole.SelectedIndex = -1

When I use looping method, I am able to set lbRole.SelectedIndex = -1
so no value is selected for the listbox. When I try the same method for
the binding, I still get a value selected.

I would like to bind, but need to be able allow input with no role
selected.

Thanks in advance...

In ASP.Net, we would, after the data bind, add a new item to the list (empty
item). Then you can set the SelectedValue/SelectedItem.

HTH,
Mythran
 
Sraydaw,

Is it not a little bit strange that when you bind a box in the listbox. You
want to bind one box to nothing, you can add an empty row in your datatable,
but that gives your mostly more problems than results.

Cor
 
Back
Top