0

i have a drop down list in my Gridview. I want to poplulate it with the values from the database ... However i am confused how to proceed ......here is my code snippet:

In the source i have written the followin code :

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" DataSourceID="AccessDataSource1" Width="515px">
            <Columns>
                 <asp:TemplateField HeaderText="My DDL Column">
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" DataSource='<%# %>'  [B]What to write here ????????????  [/B]
                 </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
               
            </Columns>
        </asp:GridView>

in dropdownlist after datasource filed do we mention the database
column names ?? n how connection is establised with database here ?? pl help me out am confused!

Cya
Rohan

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Ana D.
0

Hi laghaterohan,

First of all, why do you need a DropDownList in the Item Template? In the ItemTemplate you only show the data, not modify it, so I don't see a reason for include the DDL in the ItemTemplate.

To create a DropDownList in the GridView you can do like this:

<asp:GridView ID="GridView2" runat="server" DataSourceID="DataSource1" DataKeyNames="ID"
        SelectedValue='<%#Bind("Name") %>'>
        <Columns>
            <asp:TemplateField HeaderText="Id">
                <ItemTemplate>
                    <%#Eval("Country")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="MyDDL" DataSourceID="DDLDataSource" DataTextField="CountryName" DataValueField="CountryID"
                    Runat="server"></asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
        <asp:SqlDataSource ID="DDLDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:server %>"
        SelectCommand="SELECT CountryID, CountryName FROM CountriesTable ORDER BY CountryName">
    </asp:SqlDataSource>

You can Bind the DDL using a SqlDataSource, for example, or you can bind in the code behind. I recommend you to bind in the code behind; it's a little bit more work, but it's easier to debug.
I hope this helps.

If you have any other questions, just ask =)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.