V
Vincent
Hi, Using asp.net 3.5 I have a Gridview named glTrucks, which contains
a template field with a dropdownlist in it called ddlTruckDrivers1.
My problem is when I go into edit mode there is a different problem
depending on what route I take:
1. If I try to edit a row that is not in the ddlTruckDrivers list I
get the following error:
“ddlTruckDrivers1 has a SelectedValue which is invalid because it does
not exist in the list of items.
Parameter name: value”
I’ve tried Intercepting it before/while it is databound by trying to
put code in the “glTrucks _RowCreated”, “glTrucks _RowUpdating”,
“ddlTruckDrivers1_DataBound”, “ddlTruckDrivers1_DataBinding”,
“ddlTruckDrivers1_PreRender” and basically telling it if the value
does not exist in the gridview to use the first value, but It still
always throws that error. I have tried to reference a lot of articles/
blogs/newsgroups with no luck, “http://weblogs.asp.net/hpreishuber/
archive/2005/10/11/427266.aspx” being the one that best pertains to my
problem, but none of the solutions either in the articles or comments
work.
Here’s how my gridview and sqldatasource are setup:
<asp:GridView ID="glTrucks" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="id" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="id" HeaderText="id"
InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="truck" HeaderText="Truck"
SortExpression="truck" />
<asp:TemplateField HeaderText="driver"
SortExpression="driver">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<
%# Bind("driver") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList ID="ddlTruckDrivers1"
runat="server" DataSourceID="sqlDrivers1"
DataTextField="driver"
DataValueField="driver"
ondatabinding="ddlTruckDrivers1_DataBinding"
ondatabound="ddlTruckDrivers1_DataBound"
onprerender="ddlTruckDrivers1_PreRender"
onselectedindexchanged="ddlTruckDrivers1_SelectedIndexChanged" >
</aspropDownList>
<asp:SqlDataSource ID="sqlDrivers1"
runat="server"
ConnectionString="<%$
ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT * FROM
[FLTranDrivers] ORDER BY [driver]">
</asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString
%>"
DeleteCommand="DELETE FROM [FLTranTrucks] WHERE [id] =
@original_id"
InsertCommand="INSERT INTO [FLTranTrucks] ([truck],
[driver]) VALUES (@truck, @driver)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [FLTranTrucks]"
UpdateCommand="UPDATE [FLTranTrucks] SET [truck] = @truck,
[driver] = @driver WHERE [id] = @original_id">
<DeleteParameters>
<asparameter Name="original_id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asparameter Name="truck" Type="String" />
<asparameter Name="driver" Type="String" />
<asparameter Name="original_id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asparameter Name="truck" Type="String" />
<asparameter Name="driver" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
Here’s my code behind:
Protected Sub glTrucks_RowCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
glTrucks.RowCreated
If e.Row.RowState.ToString = "Edit" Then
If Not
IsNothing(CType(e.Row.FindControl("ddlTruckDrivers1"),
DropDownList).Items.FindByValue(DataBinder.Eval(e.Row.DataItem,
"driver").ToString())) Then
CType(e.Row.FindControl("ddlTruckDrivers1"),
DropDownList).SelectedItem.Value = DataBinder.Eval(e.Row.DataItem,
"driver").ToString()
End If
End If
End Sub
Protected Sub glTrucks_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles
glTrucks.RowUpdating
e.NewValues("driver") =
CType(glTrucks.Rows(e.RowIndex).FindControl("ddlTruckDrivers1"),
DropDownList).SelectedValue
End Sub
2. So I got frustrated and figured I would unbind ddlTruckDrivers1
but i cannot seem to figure out how to get it to set the value that
already exists in the DropDownList. I’ve tried in every event and I
cannot seem to figure out the problem
Now my only solution is to anihilate the person that asked me to
create this Any help would be appreciated. Thanks a lot. –Vincent.
a template field with a dropdownlist in it called ddlTruckDrivers1.
My problem is when I go into edit mode there is a different problem
depending on what route I take:
1. If I try to edit a row that is not in the ddlTruckDrivers list I
get the following error:
“ddlTruckDrivers1 has a SelectedValue which is invalid because it does
not exist in the list of items.
Parameter name: value”
I’ve tried Intercepting it before/while it is databound by trying to
put code in the “glTrucks _RowCreated”, “glTrucks _RowUpdating”,
“ddlTruckDrivers1_DataBound”, “ddlTruckDrivers1_DataBinding”,
“ddlTruckDrivers1_PreRender” and basically telling it if the value
does not exist in the gridview to use the first value, but It still
always throws that error. I have tried to reference a lot of articles/
blogs/newsgroups with no luck, “http://weblogs.asp.net/hpreishuber/
archive/2005/10/11/427266.aspx” being the one that best pertains to my
problem, but none of the solutions either in the articles or comments
work.
Here’s how my gridview and sqldatasource are setup:
<asp:GridView ID="glTrucks" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="id" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="id" HeaderText="id"
InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="truck" HeaderText="Truck"
SortExpression="truck" />
<asp:TemplateField HeaderText="driver"
SortExpression="driver">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<
%# Bind("driver") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<aspropDownList ID="ddlTruckDrivers1"
runat="server" DataSourceID="sqlDrivers1"
DataTextField="driver"
DataValueField="driver"
ondatabinding="ddlTruckDrivers1_DataBinding"
ondatabound="ddlTruckDrivers1_DataBound"
onprerender="ddlTruckDrivers1_PreRender"
onselectedindexchanged="ddlTruckDrivers1_SelectedIndexChanged" >
</aspropDownList>
<asp:SqlDataSource ID="sqlDrivers1"
runat="server"
ConnectionString="<%$
ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT * FROM
[FLTranDrivers] ORDER BY [driver]">
</asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString
%>"
DeleteCommand="DELETE FROM [FLTranTrucks] WHERE [id] =
@original_id"
InsertCommand="INSERT INTO [FLTranTrucks] ([truck],
[driver]) VALUES (@truck, @driver)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT * FROM [FLTranTrucks]"
UpdateCommand="UPDATE [FLTranTrucks] SET [truck] = @truck,
[driver] = @driver WHERE [id] = @original_id">
<DeleteParameters>
<asparameter Name="original_id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asparameter Name="truck" Type="String" />
<asparameter Name="driver" Type="String" />
<asparameter Name="original_id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asparameter Name="truck" Type="String" />
<asparameter Name="driver" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
Here’s my code behind:
Protected Sub glTrucks_RowCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
glTrucks.RowCreated
If e.Row.RowState.ToString = "Edit" Then
If Not
IsNothing(CType(e.Row.FindControl("ddlTruckDrivers1"),
DropDownList).Items.FindByValue(DataBinder.Eval(e.Row.DataItem,
"driver").ToString())) Then
CType(e.Row.FindControl("ddlTruckDrivers1"),
DropDownList).SelectedItem.Value = DataBinder.Eval(e.Row.DataItem,
"driver").ToString()
End If
End If
End Sub
Protected Sub glTrucks_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles
glTrucks.RowUpdating
e.NewValues("driver") =
CType(glTrucks.Rows(e.RowIndex).FindControl("ddlTruckDrivers1"),
DropDownList).SelectedValue
End Sub
2. So I got frustrated and figured I would unbind ddlTruckDrivers1
but i cannot seem to figure out how to get it to set the value that
already exists in the DropDownList. I’ve tried in every event and I
cannot seem to figure out the problem
Now my only solution is to anihilate the person that asked me to
create this Any help would be appreciated. Thanks a lot. –Vincent.