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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding the Initial Value of a DropDownList in Editable Datagrid

Status
Not open for further replies.

yu217171

Programmer
Aug 2, 2002
203
CA
Hi everyone,

I'm having a tedious problem that I'm trying to figure out. I would like to set the initial value of a dropdownlist when the user goes into Edit mode in a datagrid.

I'm trying to access the dropdownlist to set the SelectedIndex in the OnEditCommand event but the dropdownlist doesn't seem to exist yet in this event.

My question is which event would I access in order to set the dropdownlist initially when the user enter into edit mode?

Thanks,

Keith
 
Here is how I do it.

<asp:TemplateColumn HeaderText="Expense Type">
<ItemTemplate>
<asp:Label id="lblExpTyp" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ExpenseType_VC") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="cboExpType" runat="server" Width="130px" DataValueField="ExpenseType_ID" DataTextField="ExpenseType_VC" DataSource="<%# GetExpTypeEdit() %>" SelectedIndex='<%# GetExpTypeIndex(Container.DataItem("ExpenseType_ID")) %>'>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>

Function GetExpTypeEdit() As DataSet
lblError.Text = ""

Try
Dim sb As SQLBean = New SQLBean
ds = sb.getExpType()
Catch ex As Exception
lblError.Text = Trim(lblError.Text & " A problem was found loading the Expense Types for editing. Please try again.")
End Try

Return ds

End Function

Function GetExpTypeIndex(ByVal strID As String) As Integer
Dim i As Integer
Dim dt As DataTable = ds.Tables("Returned")
For i = 0 To dt.Rows.Count - 1
If Int32.Parse(strID) = Int32.Parse(dt.Rows(i)("ExpenseType_ID")) Then
Return i
End If
Next i
End Function

Hope this helps.

Hope everyone is having a great day!

Thanks - Jennifer
 
Hi Jen,

You don't access your controls through the ItemDataBound event? I thought that was the only way of accessing the controls. (when they are created and bound to the datagrid)

Keith
 
I think that you can do that as well but it is more work. The only time I have done it in the ItemDataBound event was when the item was in a nested datagrid.

Always more than one...

Hope everyone is having a great day!

Thanks - Jennifer
 
When you bind the DDL is when you set the value...

ive added items to DDL by adding to code after you close the Dataset...
Code:
...
      dropdown.databind()
   objConnection.Close()
   dropdown.items.insert(0, new ListItem("Select An Item")
end sub
Thats where it gets added...
if you want to select an item already in list...
Code:
'after databind and dataconn close
'Get rid of any selection
   DropDown.Items(DropDown.SelectedIndex).Selected = False
'Find the item your looking for...
   Dim foundItem As ListItem = Employees.Items.FindByValue(strVariable)
'make it the selected item
  If Not foundItem Is Nothing Then
     foundItem.Selected = True
  End If
 
oops...
'Employees.Items.FindByValue(strVariable)

should be
dropdown.items

dropdown is the name of the asp:ddl employees was the name of my asp:ddl - trying to shorthand!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top