Assigning a DropDownList Value in a ASP.NET GridView Using ObjectDataSource

As a moderator in the forums, I often see the same or similar questions.  The answer to this question is pretty straight forward but not 100% obvious so I thought I’d do a post about in the hopes that with a couple key words, people will find the answer.  The title basically says it all.  We have a GridView that has a data column of type bool.  We want to display in the GridView Yes or No depending on whether the data value is true or false.  The example I’m showing does not help with making this an editable field (maybe a theme for another post if this one is popular) but simply shows yes or no.

The solution involves first dropping a GridView and ObjectDataSource onto your design surface in Visual Studio.  Then, using the little chevron on the GridView, choose edit columns and convert the column you are interested in making a DropDownList into a template.  From there replace the ItemTemplate with the DropDownList code below.  You get the code below.


<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<script runat="server">
<html xmlns="">
<head runat="server">
    <title>ODS DDL Example</title>
    <form id="form1" runat="server">
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetMembers"
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
                <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                <asp:TemplateField HeaderText="Approved" SortExpression="Approved">
                        <asp:DropDownList ID="DropDownListUser" runat="server" AutoPostBack="False" 
                            SelectedValue='<%# Bind("Approved") %>'>
                            <asp:ListItem Text="Yes" Value="True"></asp:ListItem>
                            <asp:ListItem Text="No" Value="False"></asp:ListItem>


And, when you run this code (assuming you have a simple business object that returns some data), you get this:



Hope this helps!

About Peter Kellner

Follow me:


  1. I come from a CICS-then-Winform development background, and I am sure stumped on this. Your example hard-codes the dropdownlist choices via “ListItem”; I need to specify them in my cs program…but I don’t want a SQL bind/datasource. In Winforms this would be a simple ComboxBox.items.Add(“Value1”)….
    What am I missing here? Thank you for any pointers.

  2. thanks
    its work fine

  3. You are freaking awesome, thank you so very much!

  4. hello.,
    code is okay but how to update yes no values in dataBase table..
    tell me the that C# Code too

    Replace original_ProductID with ProductID

  6. Excelent! A lot of time saving.
    “the simplest solution is allways the best solution”

  7. This is indeed very helpful – thanks. I am more ambitious. I want to have a footer row for inputting additional records into the database – fairly well documented. But I want the cell in the footer row to have a DropDownList populated by the existing records in the column – again fairly well documented. This forces them to use existing categories rather than invent a new one.

    Where I am getting stuck is that I want the selected value in the DropDownList to be the same as in the first row of the Gridview, to give the user a default that they may wish to copy or to change. I can pick up the value of the top left cell with:
    CType(GridView1.Rows(0).Cells(0).FindControl(“Label1”), Label).Text

    Can you suggest what code I put where to get the DropDownList18.SelectedValue to be set to the above.

    Many thanks, Tom

  8. Peter Kellner says:

    You are correct. The fields DataTextField=”Username” and DataValueField=”id” are not necessary. They came along by accident with a cut and paste. Thanks for pointing this out.

  9. This is very helpful…thanks! My only thought is that I don’t think the DataTextField is needed, or if it is needed then it is not intuitive in the example to have a username field mentioned when it will be a boolean yes/no. Took me a second to figure out what you were trying to do with the dropdownlist.

    I’d love to see the second installment on editing that boolean!


Get every new post delivered to your Inbox

Join other followers: