Combo Box in GridView

Discussion in 'ASP .Net' started by Peter, Feb 12, 2009.

  1. Peter

    Peter Guest

    I have a DropDown Listbox in a GridView as a template field, and everything
    is working fine with exception when I try to edit the GridView and the
    gridview field data is not one of the values in the DropDown Listbox, I get
    the following error.

    [ArgumentOutOfRangeException: 'DropDownList2' has a SelectedValue which is
    invalid because it does not exist in the list of items.

    How can I have the inplace editing working in the GridView even when the
    gridview data is not one of the values of the DropDown list box?


    Thank You


    Peter
     
    Peter, Feb 12, 2009
    #1
    1. Advertising

  2. As the error says, a ddl can't be set in a value not included in the items.
    What do you want achieve? Perhaps you need some other control.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin


    "Peter" <> wrote in message
    news:...
    >I have a DropDown Listbox in a GridView as a template field, and everything
    >is working fine with exception when I try to edit the GridView and the
    >gridview field data is not one of the values in the DropDown Listbox, I get
    >the following error.
    >
    > [ArgumentOutOfRangeException: 'DropDownList2' has a SelectedValue which is
    > invalid because it does not exist in the list of items.
    >
    > How can I have the inplace editing working in the GridView even when the
    > gridview data is not one of the values of the DropDown list box?
    >
    >
    > Thank You
    >
    >
    > Peter
    >
     
    Eliyahu Goldin, Feb 12, 2009
    #2
    1. Advertising

  3. Hello peter,

    Based on my understanding, you bound the "SelectedValue" property of
    DropDownList by the datasource, and you encountered this error when you
    clicked the "update" button in edit mode of GridView. Because the updated
    GridView field data is not one of the values in DropDownList control.
    Please feel free to let me know if I've misunderstood anything.
    Could you please let me know how did you build the DropDownList? By static
    ListItem or by datasource bound? If you bound DropDownList by static
    ListItem, I suggest you bind it by a datasource. In this way, the new item
    needs to be added in the datasource of DropDownList after you update the
    GridView field so that the new item will be as one of the values of
    DropDownList.

    For example, we can create a DataSource control for DownDropList to bind
    the same data table as GridView. After GridView field data is updated, the
    related data table will be updated, which will impact the DropDownList.

    Assuming that the following is the datasource of GridView:
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT * FROM [tbl]"
    DeleteCommand="DELETE FROM [tbl] WHERE [id] = @id"
    InsertCommand="INSERT INTO [tbl] ([link], [txt]) VALUES (@link,
    @ txt)"
    UpdateCommand="UPDATE [tbl] SET [link] = @link WHERE [id] = @id
    ">
    <DeleteParameters>
    <asp:parameter Name="id" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Name="link" Type="String" />
    <asp:parameter Name="txt" Type="String" />
    <asp:parameter Name="id" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
    <asp:parameter Name="link" Type="String" />
    <asp:parameter Name="txt" Type="String" />
    </InsertParameters>
    </asp:SqlDataSource>

    Assuming that the field "link" is the value field of the DropDownList , we
    can create the SqlDataSource as below:
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString
    %>"
    SelectCommand="SELECT DISTINCT [link] FROM [tbl]">
    </asp:SqlDataSource>

    Then, we can bind it on DropDownList:
    <asp:DropDownList ID="DropDownList1" runat="server"
    DataSourceID="SqlDataSource2" DataTextField="link" DataValueField="link"
    SelectedValue='<%# Bind("link") %>' >
    </asp:DropDownList>

    In this way, after you update the GridView field data which is associated
    with "link" field, the table "tbt" will be updated. The items of
    DropDownList will be updated.

    If the above can't help you out. Please post some code snippet.


    Sincerely,

    Vince Xu

    Microsoft Online Support

    £½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    MSDN Managed Newsgroup support offering is for non-urgent issues where an
    initial response from the community or a Microsoft Support Engineer within
    2 business day is acceptable. Please note that each follow up response may
    take approximately 2 business days as the support professional working with
    you may need further investigation to reach the most efficient resolution.
    The offering is not appropriate for situations that require urgent,
    real-time or phone-based interactions. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx

    £½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½
     
    Vince Xu [MSFT], Feb 12, 2009
    #3
  4. Peter

    Peter Guest

    The problem is the DropDown List items come from one table and the dataview
    rows come from another table.

    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT DISTINCT [link] FROM [tbl2]">
    </asp:SqlDataSource

    I can't use the same table because than the DropDown List will not contain
    all of available items.

    For example if I use State list:

    If I use only one table and if tbl1 has one record and in that record the
    state is 'CA' and we run the program the DropDown List box will contain
    only 1 state instead of all of the states.

    Also some other application can enter anything for the State like 'XX' or
    'XY' so if the DropDown list is using tbl2 as it's data source it will not
    have the 'XX' in the list - and that's when the error appears. I don't want
    the error message, I want the user to be able to change it to anything in
    the DropDown List box or leave it the way it is.


    Thank You for your help!


    "Vince Xu [MSFT]" <> wrote in message
    news:...
    > Hello peter,
    >
    > Based on my understanding, you bound the "SelectedValue" property of
    > DropDownList by the datasource, and you encountered this error when you
    > clicked the "update" button in edit mode of GridView. Because the updated
    > GridView field data is not one of the values in DropDownList control.
    > Please feel free to let me know if I've misunderstood anything.
    > Could you please let me know how did you build the DropDownList? By static
    > ListItem or by datasource bound? If you bound DropDownList by static
    > ListItem, I suggest you bind it by a datasource. In this way, the new item
    > needs to be added in the datasource of DropDownList after you update the
    > GridView field so that the new item will be as one of the values of
    > DropDownList.
    >
    > For example, we can create a DataSource control for DownDropList to bind
    > the same data table as GridView. After GridView field data is updated, the
    > related data table will be updated, which will impact the DropDownList.
    >
    > Assuming that the following is the datasource of GridView:
    > <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    > ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    > SelectCommand="SELECT * FROM [tbl]"
    > DeleteCommand="DELETE FROM [tbl] WHERE [id] = @id"
    > InsertCommand="INSERT INTO [tbl] ([link], [txt]) VALUES (@link,
    > @ txt)"
    > UpdateCommand="UPDATE [tbl] SET [link] = @link WHERE [id] = @id
    > ">
    > <DeleteParameters>
    > <asp:parameter Name="id" Type="Int32" />
    > </DeleteParameters>
    > <UpdateParameters>
    > <asp:parameter Name="link" Type="String" />
    > <asp:parameter Name="txt" Type="String" />
    > <asp:parameter Name="id" Type="Int32" />
    > </UpdateParameters>
    > <InsertParameters>
    > <asp:parameter Name="link" Type="String" />
    > <asp:parameter Name="txt" Type="String" />
    > </InsertParameters>
    > </asp:SqlDataSource>
    >
    > Assuming that the field "link" is the value field of the DropDownList ,
    > we
    > can create the SqlDataSource as below:
    > <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    > ConnectionString="<%$ ConnectionStrings:ConnectionString
    > %>"
    > SelectCommand="SELECT DISTINCT [link] FROM [tbl]">
    > </asp:SqlDataSource>
    >
    > Then, we can bind it on DropDownList:
    > <asp:DropDownList ID="DropDownList1" runat="server"
    > DataSourceID="SqlDataSource2" DataTextField="link" DataValueField="link"
    > SelectedValue='<%# Bind("link") %>' >
    > </asp:DropDownList>
    >
    > In this way, after you update the GridView field data which is associated
    > with "link" field, the table "tbt" will be updated. The items of
    > DropDownList will be updated.
    >
    > If the above can't help you out. Please post some code snippet.
    >
    >
    > Sincerely,
    >
    > Vince Xu
    >
    > Microsoft Online Support
    >
    > £½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½
    >
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >
    > MSDN Managed Newsgroup support offering is for non-urgent issues where an
    > initial response from the community or a Microsoft Support Engineer within
    > 2 business day is acceptable. Please note that each follow up response may
    > take approximately 2 business days as the support professional working
    > with
    > you may need further investigation to reach the most efficient resolution.
    > The offering is not appropriate for situations that require urgent,
    > real-time or phone-based interactions. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    >
    > £½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½£½
    >
     
    Peter, Feb 12, 2009
    #4
  5. Hello Peter,

    Based on my understanding, you used two different data sources for
    DropDownList and GridView. After the data is updated, the datasource of
    DropDownList was not updated. So the error appears. You want to let user to
    choose an available item again in this scenario. If I have misunderstood
    you, please feel free to let me know.

    To resolve this problem, I think there are two approaches we can use.


    1. You can add a new item which the user typed into the datasource of
    DropDownList in OnRowUpdated event of GridView. In this way, if the data is
    not one of the values in DropDownList, the data will be added into tbl2 and
    DropDownList will be updated.
    protected void GridView1_RowUpdated(object sender,
    GridViewUpdatedEventArgs e)
    {
    //Add a new item which the user typed into tbl2. You can use
    e.NewValues[index] to get the data in related GridView field.
    }

    2.If you want the user choose another available item in DropDownList if the
    value isn't one of the values in DropDownList control, rather than add the
    new item which the user typed in GridView Field into the DropDownList, you
    can add an additional item(say "Please select an item") in DropDownList so
    that it can select this item when no items is matching.

    When can use a method to bind on SelectedBalue property in this way:
    <asp:DropDownList ID="DropDownList1" runat="server"
    AppendDataBoundItems="true" DataSourceID="SqlDataSource2"
    DataTextField="linkstr" DataValueField="linkstr" SelectedValue='<%#
    bindstr(Eval("link").ToString()) %>' >
    <asp:ListItem Value="null">=== Please selecte an
    item ===</asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString
    %>"
    SelectCommand="SELECT [linkstr] FROM [tbl2]">
    </asp:SqlDataSource>

    Code behind:
    public string bindstr(string value)
    {
    //Check if the value is existing in tbl2.
    if ("the value is existing in tbl2")
    {
    return value;
    }
    return "null";
    }

    Sincerely,

    Vince Xu

    Microsoft Online Support
     
    Vince Xu [MSFT], Feb 13, 2009
    #5
  6. how to retrive data from another table

    I want data like(party name from party table) in combo box

    please tell me the code or solution



    Peter wrote:

    The problem is the DropDown List items come from one table and the dataview
    12-Feb-09

    The problem is the DropDown List items come from one table and the datavie
    rows come from another table

    <asp:SqlDataSource ID="SqlDataSource2" runat="server
    ConnectionString="<%$ ConnectionStrings

    Previous Posts In This Thread:

    On Wednesday, February 11, 2009 10:51 PM
    Peter wrote:

    Combo Box in GridView
    I have a DropDown Listbox in a GridView as a template field, and everythin
    is working fine with exception when I try to edit the GridView and th
    gridview field data is not one of the values in the D

    On Thursday, February 12, 2009 2:05 AM
    Eliyahu Goldin wrote:

    As the error says, a ddl can't be set in a value not included in the items.
    As the error says, a ddl cannot be set in a value not included in the items
    What do you want achieve? Perhaps you need some other control

    -
    Eliyahu Goldin
    Software Develope
    Microsoft MVP [ASP.NE

    On Thursday, February 12, 2009 2:54 AM
    v-vince wrote:

    Hello peter,Based on my understanding, you bound the "SelectedValue" property
    Hello peter

    Based on my understanding, you bound the "SelectedValue" property o
    DropDownList by the datasource, and you encountered this error when yo
    clicked the "update" button in edit mode of G

    On Thursday, February 12, 2009 9:45 AM
    Peter wrote:

    The problem is the DropDown List items come from one table and the dataview
    The problem is the DropDown List items come from one table and the datavie
    rows come from another table

    <asp:SqlDataSource ID="SqlDataSource2" runat="server
    ConnectionString="<%$ ConnectionStrings

    On Friday, February 13, 2009 1:09 AM
    v-vince wrote:

    Hello Peter,Based on my understanding, you used two different data sources for
    Hello Peter

    Based on my understanding, you used two different data sources fo
    DropDownList and GridView. After the data is updated, the datasource o
    DropDownList was not updated. So the error appe

    EggHeadCafe - Software Developer Portal of Choice
    ASP.NET DataGrid Paging at the Server
    http://www.eggheadcafe.com/tutorial...6e-b400bd59855b/aspnet-datagrid-paging-a.aspx
     
    snehal katkar, Oct 19, 2009
    #6
  7. Re: how to retrive data from another table

    On Oct 19, 12:55 pm, snehal katkar wrote:
    > I want data like(party name from party table) in combo box
    >
    > please tell me the code or solution
    >


    Can you please provide more details on it?
     
    Alexey Smirnov, Oct 19, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QW5nZWw=?=

    COMBO BOX INSTEAD OF DROP DOWN BOX

    =?Utf-8?B?QW5nZWw=?=, Jun 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    653
    Ken Cox [Microsoft MVP]
    Jun 29, 2004
  2. Adrian
    Replies:
    1
    Views:
    249
    Bullschmidt
    Feb 18, 2004
  3. dthmtlgod

    Fill Combo Box based on another Combo Box

    dthmtlgod, Nov 16, 2005, in forum: ASP General
    Replies:
    1
    Views:
    624
    Bob Barrows [MVP]
    Nov 16, 2005
  4. Mike

    Combo Box and text box

    Mike, Sep 24, 2003, in forum: Javascript
    Replies:
    2
    Views:
    181
  5. SM
    Replies:
    2
    Views:
    403
Loading...

Share This Page