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

DropDownList as an EditItemTemplate in a DataGrid

Status
Not open for further replies.

harmmeijer

Programmer
Mar 1, 2001
869
CN
The url:
shows us how to do a one to many relationship in an updatable DataGrid.
This works for one to many not for one or zero to many, because the entire table is dumped in the dropdownlist.
If the DropDownList were to be filled with a rooms table the user is forced to choose a room since an empty record is not included in the DropDownList.


If this is my aspx page:
<%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;WebForm2.aspx.vb&quot; Inherits=&quot;mrrs.WebForm2&quot; trace=&quot;true&quot;%>
<form id=&quot;form1&quot; runat=&quot;server&quot;>
<asp:DataGrid id=&quot;dgPlanner&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot; OnEditCommand=&quot;dgPlannerEdit&quot; OnCancelCommand=&quot;dgPlannerCancel&quot;>
<Columns>
<asp:TemplateColumn HeaderText=&quot;<b>Room</b>&quot;>
<ItemTemplate>
<%# Container.DataItem(&quot;RoomName&quot;) %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList
id=&quot;lstRooms&quot;
runat=&quot;server&quot;
DataValueField=&quot;ID&quot;
DataTextField=&quot;RoomName&quot; OnRenderContents = &quot;selRender&quot;
DataSource=&quot;<%# GetCategories() %>&quot; even if I try to get the dropdown in the getCategories function it is nothing, so adding the empty record in this function won't work
/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText=&quot;Edit&quot; UpdateText=&quot;<b>Update</b>&quot; CancelText=&quot;<b>Cancel</b>&quot; HeaderText=&quot;<b>Edit</b>&quot; />
</Columns>
</asp:DataGrid>
</form>

And I am using the ItemDataBound of the dgPlanner lstRooms is never anything else than nothing, here is the code behind:

Protected WithEvents dgPlanner As System.Web.UI.WebControls.DataGrid
...
Private Sub ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgPlanner.ItemDataBound
Dim intRow As Int16 = e.Item.ItemIndex
Dim lngSelectedID As Long = 0
If intRow > -1 Then
If intRow = dgPlanner.EditItemIndex Then
Dim str As String = &quot;&quot;
lngSelectedID = e.Item.DataItem(&quot;requestID&quot;)
' useless to try so why bother, it will allways be nothing Page.FindControl(&quot;lstRooms&quot;)
' if I could get my hands on the lstRooms here I could insert the table and an empty record in the dropdown
End If
End If
End Sub


The question is:
Where do I insert the empty record in the dropdownlist?
 
answer:

Private Sub ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgPlanner.ItemDataBound
Dim intRow As Int16 = e.Item.ItemIndex
Dim lngSelectedID As Long = 0
Dim obj As Object
If intRow > -1 Then
If intRow = dgPlanner.EditItemIndex Then
lngSelectedID = e.Item.DataItem(&quot;requestID&quot;)
Try
obj = e.Item.Cells(1).FindControl(&quot;lstRooms&quot;)
If IsNothing(obj) Then
obj = Nothing
Else
lstRooms = CType(obj, DropDownList)
lstRooms.Items.Insert(0, &quot;item1&quot;)
lstRooms.Items.Insert(1, &quot;item2&quot;)
End If
Catch
End Try
End If
End If
End Sub
 
Using cells won't allowe you to add a column without changing all your code, this might be a better way:
'obj = e.Item.Cells(1).FindControl(&quot;lstRooms&quot;) no, this is dependent on the amount of colomns
obj = e.Item.FindControl(&quot;lstRooms&quot;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top