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:DropDownLi st ID="cboActivity Time" runat="server"
AppendDataBound Items=”true
DataSourceID="o bjTimes" DataValueField= "TimeShow" DataTextField=" TimeShow"
SelectedValue=' <%# Bind("ActivityT ime") %>' ></asp:DropDownLis t>
http://msdn2.microsoft.com/en-us/lib...ms(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.Da taBound event, e.g.
Protected Sub cboAcitvityTime _DataBound(ByVa l sender As Object, ByVal e As
EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim gvr As GridViewRow = CType(ddl.Namin gContainer, GridViewRow)
If Not gvr.DataItem Is Nothing Then
Dim strTimeShow As String = CType(gvr.DataI tem, 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.ClearSelect ion()
Dim li As ListItem = ddl.Items.FindB yValue(strTimeS how)
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 GridViewRowUpda ting, like this:
Protected Sub grdActivity_Row Updating (ByVal sender As Object, ByVal e As
GridViewUpdateE ventArgs)
Dim strTimeShow As String = CType(CType(sen der,
GridViewRow).Fi ndControl("cboA ctivityTime"), DropDownList).S electedValue
e.NewValues("Ac tivityTime") = 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/lib...ng(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.
--
HTH,
Phillip Williams
http://www.societopia.net http://www.webswapp.com
"Jakob Lithner" wrote:
Thanks Phillip, I appreciate your effort!
1) I tried hard to get this working but one remaining stumblingblock is this
line:
<asp:DropDownLi st ID="cboActivity Time" runat="server"
DataSourceID="o bjTimes" DataValueField= "TimeShow" DataTextField=" TimeShow"
SelectedValue=' <%# Bind("ActivityT ime") %>' ></asp:DropDownLis t>
It will cause this error:
"'cboActivityTi me' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value"
Possible workaoround?
Your solution obviously works. What is the difference? My ObjectDatasourc e
retrieves a DataView, will that be a problem?
2) And I am still VERY eager to manipulate the DropDownList in code behind
(Load, set SelectedValue, etc). I am much more used to plain code than these
ObjectSource objects, and find it more flexible.
In the GridView RowEditing Event I can easily get hold of the Literal
control of the TemplateField. Is it impossible to get hold of the
DropDownList control in a similar way?
<asp:TemplateFi eld HeaderText="Act ivityTime">
<ItemTemplate >
<asp:Literal ID="litActivity Time" runat="server"
Text='<%# Eval("ActivityT ime") %>'></asp:Literal>
</ItemTemplate>
<EditItemTempla te>
<asp:ObjectData Source ID="objTimes" runat="server"
TypeName="SKF.B usinessLogic.St aticInfo"
SelectMethod="G etNormalTimes"
DataObjectTypeN ame="SKF.Busine ssLogic.StaticI nfo"></asp:ObjectDataS ource>
<asp:DropDownLi st ID="cboActivity Time" runat="server"
DataSourceID="o bjTimes" DataValueField= "TimeShow" DataTextField=" TimeShow"
SelectedValue=' <%# Bind("ActivityT ime") %>' ></asp:DropDownLis t>
</EditItemTemplat e>
</asp:TemplateFie ld>
Protected Sub grdActivity_Row Editing(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewEditEventAr gs) Handles
grdActivity.Row Editing
Dim grd As GridView
Dim row As GridViewRow
Dim lit As New Literal
Dim cbo As DropDownList
grd = DirectCast(send er, GridView)
row = DirectCast(send er, GridView).Rows( e.NewEditIndex)
lit = DirectCast(row. FindControl("li tActivityTime") , Literal)
'cbo = DirectCast(row. FindControl("cb oActivityTime") , DropDownList)
End Sub