Hello Jakob,
Since you are using Bind (which will set the selected value from the data as
well as save the selected value to data upon update), the dropdownlist must
be populated with all possible values. In your specific code, for example,
if you had a value in the field named “ActivityTime†that is not within the
set of values for “TimeShow†then you would get that error. It can also
happen if your datatable schema was created to allow nulls in ActivityTime.
The solutions are:
1- if the problem is happening due to Null values in AcitvityTime then you
can declare the dropdownlist like this:
<asp
ropDownList ID="cboActivityTime" runat="server"
AppendDataBoundItems=â€trueâ€
DataSourceID="objTimes" DataValueField="TimeShow" DataTextField="TimeShow"
SelectedValue='<%# Bind("ActivityTime") %>' ></asp
ropDownList>
http://msdn2.microsoft.com/en-us/li....listcontrol.appenddatabounditems(VS.80).aspx
2- if the problem is happening due to illegal values saved in “ActivityTimeâ€
(that are not part of the set of values for TimeShow) then you can:
a. fix the data
b. create a UNION query within the objTimes that would bring not only the
values from the “TimeShow†but also any values in “ActivityTime†that is not
included
c. handle it on the level of the user interface like I did on my sample
where I removed the Bind statement from the DropDownList and replaced with
code within the dropdownlist.DataBound event, e.g.
Protected Sub cboAcitvityTime_DataBound(ByVal sender As Object, ByVal e As
EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim gvr As GridViewRow = CType(ddl.NamingContainer, GridViewRow)
If Not gvr.DataItem Is Nothing Then
Dim strTimeShow As String = CType(gvr.DataItem, DataRowView)("
TimeShow")
'be careful of the possibility that the value saved on the
'database does not exist in the valid selections that are
displayed
'on the list
ddl.ClearSelection()
Dim li As ListItem = ddl.Items.FindByValue(strTimeShow)
If Not li Is Nothing Then li.Selected = True
End If
In which case you would also have to save the selected value upon update by
consuming the GridViewRowUpdating, like this:
Protected Sub grdActivity_RowUpdating (ByVal sender As Object, ByVal e As
GridViewUpdateEventArgs)
Dim strTimeShow As String = CType(CType(sender,
GridViewRow).FindControl("cboActivityTime"), DropDownList).SelectedValue
e.NewValues("ActivityTime") = strTimeShow
e.Cancel = False
End Sub
Regarding your question on how to find the dropdownlist within the
GridViewRow while handling the RowEditing event…
The RowEditing event is raised when a row's Edit button is clicked, but
before the GridView control enters edit mode.
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowediting(VS.80).aspx
Which means that during this event processing the dropdownlist has not yet
been rendered. You can find that dropdownlist after the edit is complete
and the user pressed on the update command which will trigger the RowUpdating
event.