DropDownList in Gridview when item does not exist in ddl

Discussion in 'ASP .Net' started by Vincent, Nov 6, 2008.

  1. Vincent

    Vincent Guest

    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>
    <asp:DropDownList ID="ddlTruckDrivers1"
    runat="server" DataSourceID="sqlDrivers1"
    DataTextField="driver"
    DataValueField="driver"

    ondatabinding="ddlTruckDrivers1_DataBinding"
    ondatabound="ddlTruckDrivers1_DataBound"
    onprerender="ddlTruckDrivers1_PreRender"

    onselectedindexchanged="ddlTruckDrivers1_SelectedIndexChanged" >
    </asp:DropDownList>

    <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>
    <asp:parameter Name="original_id" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="truck" Type="String" />
    <asp:parameter Name="driver" Type="String" />
    <asp:parameter Name="original_id" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
    <asp:parameter Name="truck" Type="String" />
    <asp:parameter 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.
     
    Vincent, Nov 6, 2008
    #1
    1. Advertisements

  2. Vincent

    Vincent Guest

    I answered my own question. Here's what I did to solve this:

    1. Deleted the "selected value="<%# bind("driver") %>" from the
    dropdownlist. (i did not have this code in my above posting)
    2. Placed "Imports System.Data" at the top of my code page to
    accomodate for the "DataRowView"
    3. Entered the following code in my dropdownlist_databound. I got
    this code from teh following website:

    http://www.webswapp.com/codesamples/aspnet20/dependentlists/default.aspx
    (he has codebehind in C# or VB)

    Protected Sub ddlTruckDrivers1_DataBound(ByVal sender As Object,
    ByVal e As System.EventArgs)
    Dim ddl As DropDownList = DirectCast(sender, DropDownList)
    '=== add an empty item on top of the list

    Dim gvRow As GridViewRow = DirectCast(ddl.NamingContainer,
    GridViewRow)
    'check to see if the item exists in the gridview, if it doesn't then
    it will default to the first item in the dropdownlist
    If Not gvRow.DataItem Is Nothing Then
    Dim strDriver As String = DirectCast(gvRow.DataItem,
    DataRowView)("driver").ToString
    '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(strDriver)
    If Not li Is Nothing Then li.Selected = True

    End If
    End Sub

    4. This small piece of data to set the value of the update parameter:

    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
     
    Vincent, Nov 6, 2008
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.