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

Dani AI

Generated

A quick clarifier to build on 's question and 's good suggestion about using the EditItemTemplate and binding in code-behind.

A DropDownList needs a data source (DataTable, List, IDataReader, etc.) and two property mappings: one column for the displayed text and one for the value. You can supply that source declaratively (an ASP.NET data source control that contains the connection string) or programmatically. In markup you link a DropDownList to an existing data-source control; in code-behind you assign a collection to the DropDownList.DataSource, set the text/value fields, and call DataBind. For editable GridView rows the usual pattern is to populate the DDL when the row enters edit mode, not in the ItemTemplate used for plain display.

A reliable code-behind pattern is to populate the lookup inside RowDataBound (only for the editing row), set SelectedValue from the row's data, and avoid rebinding on every postback. Example pattern:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.DataRow) return;
    if ((e.Row.RowState & DataControlRowState.Edit) == 0) return;

    var ddl = (DropDownList)e.Row.FindControl("DropDownList1");
    if (ddl == null) return;

    var lookup = GetLookupTable(); // returns DataTable/List from DB or cache
    ddl.DataSource = lookup;
    ddl.DataTextField = "Name";
    ddl.DataValueField = "Id";
    ddl.DataBind();

    var current = DataBinder.Eval(e.Row.DataItem, "LookupId");
    if (current != null) ddl.SelectedValue = current.ToString();
}

Troubleshooting and best practices: keep connection strings in web.config and use parameterized queries or an ORM; wrap ADO.NET objects in using blocks; avoid rebinding the lookup on every postback (cache it); ensure GridView DataKeyNames includes the PK if you need the row id on update; handle missing values before assigning SelectedValue to avoid exceptions. If the list is purely for display, bind the display name into the ItemTemplate (label/text) instead of rendering a DropDownList for every row to save resources.

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 =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.