locked
how to add items in dropdownlist in gridview while editing RRS feed

  • Question

  • User1835225731 posted

    hi all,

    I have 1 datagridview and I put a dropdownlist in the gridview. When I want to edit the data in gridview, the item should appear as dropdownlist. Yes, the item appear in dropdownlist. The problem is, it should appear all the items but it only appear the data in the database. How to make all the items appear in the dropdownlist? Below is my coding.

    <asp:TemplateField HeaderText="Membership_Type" 
                                SortExpression="Membership_Type">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="DropDownList1" runat="server" 
                                        DataSourceID="SqlDataSource5" DataTextField="Membership_Type" 
                                        DataValueField="Membership_Type" 
                                        SelectedValue='<%# Bind("Membership_Type") %>' Text='<%# Bind("Membership_Type") %>' AutoPostBack="True">
                                        <asp:ListItem Value="1" Text="Priviledge" />
                                        <asp:ListItem Value="2" Text="Silver" />
                                        <asp:ListItem Value="3" Text="Gold" />
                                        <asp:ListItem Value="4" Text="Platinum" />
                                    </asp:DropDownList>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Membership_Type") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
    
    <asp:SqlDataSource ID="SqlDataSource5" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:testCRMConnectionString %>" 
                        SelectCommand="SELECT DISTINCT [Membership_Type] FROM [MembershipDetails]">
                    </asp:SqlDataSource>

    Can anyone help me with this code?

    Friday, September 13, 2013 5:13 AM

Answers

  • User-183374066 posted

    Try AppendDataBoundItems with viewstate off on dropdownlist

    <asp:DropDownList ID="DropDownList1" runat="server"
                                        DataSourceID="SqlDataSource5" DataTextField="Membership_Type" EnableViewState="false"
                                        DataValueField="Membership_Type" AppendDataBoundItems="true"
                                        SelectedValue='<%# Bind("Membership_Type") %>' Text='<%# Bind("Membership_Type") %>' AutoPostBack="True">
                                        <asp:ListItem Value="1" Text="Priviledge" />
                                        <asp:ListItem Value="2" Text="Silver" />
                                        <asp:ListItem Value="3" Text="Gold" />
                                        <asp:ListItem Value="4" Text="Platinum" />
                                    </asp:DropDownList>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 13, 2013 6:19 AM
  • User-1716253493 posted

    Seem like you want to add ddl items manualy, remove DataSourceId and DataField.

    You cant bind (two way binding) with same field name to another property

    <EditItemTemplate>
       <asp:DropDownList ID="DropDownList1" runat="server"
           SelectedValue='<%# Bind("Membership_Type") %>'>
               <asp:ListItem Value="1" Text="Priviledge" />
               <asp:ListItem Value="2" Text="Silver" />
               <asp:ListItem Value="3" Text="Gold" />
               <asp:ListItem Value="4" Text="Platinum" />
       </asp:DropDownList>
    </EditItemTemplate>


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 15, 2013 8:18 PM

All replies

  • User-183374066 posted

    Try AppendDataBoundItems with viewstate off on dropdownlist

    <asp:DropDownList ID="DropDownList1" runat="server"
                                        DataSourceID="SqlDataSource5" DataTextField="Membership_Type" EnableViewState="false"
                                        DataValueField="Membership_Type" AppendDataBoundItems="true"
                                        SelectedValue='<%# Bind("Membership_Type") %>' Text='<%# Bind("Membership_Type") %>' AutoPostBack="True">
                                        <asp:ListItem Value="1" Text="Priviledge" />
                                        <asp:ListItem Value="2" Text="Silver" />
                                        <asp:ListItem Value="3" Text="Gold" />
                                        <asp:ListItem Value="4" Text="Platinum" />
                                    </asp:DropDownList>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 13, 2013 6:19 AM
  • User-1716253493 posted

    Seem like you want to add ddl items manualy, remove DataSourceId and DataField.

    You cant bind (two way binding) with same field name to another property

    <EditItemTemplate>
       <asp:DropDownList ID="DropDownList1" runat="server"
           SelectedValue='<%# Bind("Membership_Type") %>'>
               <asp:ListItem Value="1" Text="Priviledge" />
               <asp:ListItem Value="2" Text="Silver" />
               <asp:ListItem Value="3" Text="Gold" />
               <asp:ListItem Value="4" Text="Platinum" />
       </asp:DropDownList>
    </EditItemTemplate>


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 15, 2013 8:18 PM