Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fill DropDownList in Edit row of DataGrid - Multiple DropDownLists

Status
Not open for further replies.

JoeZim

MIS
Sep 11, 2003
87
US
I've gotten this to work for one DataGrid DropDownList, but can't figure out how to get it working when there are two DataGrid DropDownLists in an edit row. Here is my code, which I got from a post linking to MSDN:

Code:
Private Sub FacilityGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles FacilityGrid.ItemDataBound 

If e.Item.ItemType = ListItemType.EditItem Then 

Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView) 

Dim currentcustname As String = CType(drv("cust_name"), String) 

Dim ddl As DropDownList 

ddl = CType(e.Item.FindControl("ddlCustomerName"), DropDownList) 

ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(currentcustname))

Dim currentfactype As String = CType(drv("facilitytype_desc"), String) 

Dim ddl1 As DropDownList 

ddl1 = CType(e.Item.FindControl("ddlFacilityType"), DropDownList) 

ddl1.SelectedIndex = ddl1.Items.IndexOf(ddl1.Items.FindByText(currentfactype))

End If 

End Sub


I have two DropdownLists in the edit row and I can't figure out how to get the second one (ddlFacilityType) to retain the current selected text when the edit command is issued. Is my approach in the above code wrong?

Thanks, Joe










 
The code should work, since you are just using the same code for both ddls, and just looking at a different column to get the value.
Are you getting any errors? Are you sure this line produces a value?
Code:
Dim currentfactype As String = CType(drv("facilitytype_desc"), String)

Jim
 
Jim, the line does produce a string value, but the dropdownlist defaults to the first item. I just can't put my finger on what may be wrong when it tries to set the selectedindex of the second ddl.
 
Are you sure the value it gets is actually in the drop down list? Maybe something is spelled wrong?

Also, put a stop on the EndIF and see what the selectedindex is of ddl1.

It seems it is not finding the value in the variable.

Jim
 
Ok, you got me on a different path that ended in the solution! The problem was the value looked like it was in the dropdownlist, but it was a difference between VarChar and Char values. So, I just had to trim the line:

Code:
Dim currentfactype As String = CType(drv("facilitytype_desc"), String).Trim

Now, the indexof statment can locate the string properly. I was not thinking along these lines at all, so thanks for your guidance!

Joe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top