views:

472

answers:

6

I have a dropdown list that I am binding to a datatable. Here is the code I am using to do it:

ddlBuildAddr.DataSource = buildings
ddlBuildAddr.DataTextField = "buildingName"
ddlBuildAddr.DataValueField = "buildingId"
Dim addressId As Int32 = OfficeData.GetInstance().GetBuildingId(currentAddress)
ddlBuildAddr.SelectedIndex = addressId
ddlBuildAddr.DataBind()

Unfortunately, the line ddlBuildAddr.SelectedIndex = addressId is failing. Looking at this line through the debugger, the SelectedIndex goes to -1, while addressId goes to 2. What gives? Why would the assignment operator flatout not work?

A: 

I think that you need to set the SelectedValue property instead.

Konamiman
SelectedValue is returning an empty string.
4501
+6  A: 

Move your ddlDeptName.DataBind() to before you try to set the selected index. Before you bind, you don't actually have any items in the dropdown so an index of 2 is invalid.

TLiebe
Stupid mistake, you are right! However, the selectedindex stil does not change.
4501
Have you verified that the dropdown is actually getting filled with anything? Try commenting out the line to set the index and verify that there are items being loaded into the list. Also, as others have suggested, you probably want to set the SelectedValue property.
TLiebe
Yes it is. I have done this and it's all filled fine :)
4501
Argh - missed the DataBind() call. Good catch!
Nicolas Webb
+2  A: 

Replace this line

ddlDeptName.SelectedIndex = addressId

With this:

ddlDeptName.SelectedValue = addressId.ToString()

As for why it's failing - addressId is likely out of the range of possible index values of your drop down list.

Nicolas Webb
taking away the ToString somehow made this work. Thank you
4501
If you take the ToString, then you come up with exactly the same thing that I suggested, which you said it did not work... huh?
Konamiman
A: 

as TLiebe has pointed out, move your databind to BEFORE you try to set the selected. the databinding method basically wipes out any previous state you have set on the combo-box.

second, the selected index is NOT the datavalue or the selected value members. it is the index of the ListItem in the dropdown's collection of items such that dropdown[ dropdown.selectedindex] will give you the item from the list that is marked as selected. so, you must find the item you want to select, then set the selectedindex to the index of that item.

a VB example from MSDN:

' Selects the item whose text is Apples
ListBox1.Items.FindByText("Apples")
If Not li Is Nothing Then
   li.Selected = True
End If

// Selects the item whose text is Apples
ListItem li = ListBox1.Items.FindByText("Apples");
if(li != null)
{
   li.Selected = true;
}
Muad'Dib
A: 

Use this

ddlBuildAddr.DataSource = buildings
ddlBuildAddr.DataTextField = "buildingName"
ddlBuildAddr.DataValueField = "buildingId"
Dim addressId As Int32 = OfficeData.GetInstance().GetBuildingId(currentAddress)

ddlBuildAddr.Databind()

After

foreach (var item in ddlBuildAddr.Items)
{
  if(Convert.toInt32(item.value)==addressId)
  {
     item.selected=true;
     break;
  }
}
ebattulga
A: 

Change your code as follows....

ddlBuildAddr.DataSource = buildings
ddlBuildAddr.DataTextField = "buildingName"
ddlBuildAddr.DataValueField = "buildingId"
ddlBuildAddr.DataBind()

Dim addressId As Int32 = OfficeData.GetInstance().GetBuildingId(currentAddress)

ddlBuildAddr.SelectedValue = addressId //It may throw error if item is not found in the list
(or)
ddlBuildAddr.Items.FindByValue(addressId).Selected = true;
(or)
ListItem lstNew = ddlBuildAddr.Items.FindByValue(addressId)
ddlBuildAddr.selectedItem = lstNew
Srinivasan
the .selecteditem property is read only :)
4501