G
Guest
It is a bit different when using a GridView .....
This is what I came up with:
<asp:GridView ID="grdActivity" runat="server"
AutoGenerateColumns="False" DataSourceID="objActivities">
<Columns>
<asp:BoundField DataField="ActivityDate"
HeaderText="ActivityDate"/>
<asp:TemplateField HeaderText="ActivityTime">
<EditItemTemplate>
<asp
ropDownList ID="cboActivityTime"
runat="server"></asp
ropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Literal ID="litActivityTime"
runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True"
ShowEditButton="True" />
</Columns>
</asp:GridView>
Protected Sub grdActivity_RowEditing(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewEditEventArgs) Handles
grdActivity.RowEditing
'Get hold of combobox
Dim row As GridViewRow = DirectCast(sender,
GridView).Rows(e.NewEditIndex)
Dim cbo As DropDownList =
DirectCast(row.FindControl("cboActivityTime"), DropDownList)
'Load the combobox
cbo.DataSource = GetAvailableTimes.DefaultView
cbo.DataValueField = "Time"
cbo.DataTextField = "TimeString"
cbo.DataBind()
End Sub
Protected Sub grdActivity_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles
grdActivity.RowUpdating
'Get hold of combobox
Dim row As GridViewRow = DirectCast(sender,
GridView).Rows(e.NewEditIndex)
Dim cbo As DropDownList =
DirectCast(row.FindControl("cboActivityTime"), DropDownList)
'Set the New value of the object
e.NewValues("ActivityTime") = cbo.SelectedValue
'Accept the value
e.Cancel = False
End Sub
The first problem is that the FindControl will not find the DropDownList,
probably because it is placed inside a TemplateField.
This is what I came up with:
<asp:GridView ID="grdActivity" runat="server"
AutoGenerateColumns="False" DataSourceID="objActivities">
<Columns>
<asp:BoundField DataField="ActivityDate"
HeaderText="ActivityDate"/>
<asp:TemplateField HeaderText="ActivityTime">
<EditItemTemplate>
<asp
runat="server"></asp
</EditItemTemplate>
<ItemTemplate>
<asp:Literal ID="litActivityTime"
runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True"
ShowEditButton="True" />
</Columns>
</asp:GridView>
Protected Sub grdActivity_RowEditing(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewEditEventArgs) Handles
grdActivity.RowEditing
'Get hold of combobox
Dim row As GridViewRow = DirectCast(sender,
GridView).Rows(e.NewEditIndex)
Dim cbo As DropDownList =
DirectCast(row.FindControl("cboActivityTime"), DropDownList)
'Load the combobox
cbo.DataSource = GetAvailableTimes.DefaultView
cbo.DataValueField = "Time"
cbo.DataTextField = "TimeString"
cbo.DataBind()
End Sub
Protected Sub grdActivity_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles
grdActivity.RowUpdating
'Get hold of combobox
Dim row As GridViewRow = DirectCast(sender,
GridView).Rows(e.NewEditIndex)
Dim cbo As DropDownList =
DirectCast(row.FindControl("cboActivityTime"), DropDownList)
'Set the New value of the object
e.NewValues("ActivityTime") = cbo.SelectedValue
'Accept the value
e.Cancel = False
End Sub
The first problem is that the FindControl will not find the DropDownList,
probably because it is placed inside a TemplateField.