Click here to Skip to main content
15,923,168 members
Please Sign up or sign in to vote.
2.60/5 (2 votes)
See more:
Hi i have to add one dropdownlist in grid view in which i have to show some list of items on edit mode how to do this any please help me here is my code.

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowcancelingEdit" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="userid" OnRowEditing="GridView1_RowEditing" >
           <Columns>
               <asp:BoundField DataField="userid" HeaderText="userid" ReadOnly="true" />
               <asp:TemplateField HeaderText="name">
               <ItemTemplate>
           <%# Eval("name")%>
         </ItemTemplate>
         <EditItemTemplate>
           <asp:TextBox runat="server" ID="txtname" Text='<%# Bind("name")%>' />
         </EditItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="age">
               <ItemTemplate>
             <%# Eval("age")%>
         </ItemTemplate>
         <EditItemTemplate>
             <asp:TextBox runat="server" ID="txtage" Text='<%# Bind("age")%>' />
         </EditItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="City" SortExpression="City">
                   <EditItemTemplate>
                       <asp:DropDownList ID="DropDownList1" runat="server"
                           DataSourceID="SqlDataSource2" DataTextField="City" DataValueField="City"
                           SelectedValue='<%# Bind("city") %>' >
                       </asp:DropDownList>
                   </EditItemTemplate>
                   <ItemTemplate>
                       <asp:Label ID="Label1" runat="server" Text='<%# Bind("city") %>'></asp:Label>
                   </ItemTemplate>
               </asp:TemplateField>
               <asp:CommandField ShowEditButton="True" ButtonType="Button" />
           </Columns>
       </asp:GridView>

       <br />

   </div>
   <asp:SqlDataSource ID="SqlDataSource2" runat="server"
       ConnectionString="Server=ALOK\SQLEXPRESS; Database=Employee; uid=sa; pwd=1234;"
       SelectCommand="SELECT DISTINCT [City] FROM [employ]"></asp:SqlDataSource>
   <br />
   <br />
   </form>
Posted
Updated 5-Jan-14 22:27pm
v2

1 solution

 
Share this answer
 
Comments
Alok.singh1166 5-Jan-14 14:55pm    
Actuly i have to add list of items in that dropdown
Maciej Los 5-Jan-14 15:01pm    
And...
Alok.singh1166 5-Jan-14 21:48pm    
i want to show a list of item in a gridview drop downlist on edit mode.How to do this please help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900