Assigning a DropDownList Value in a ASP.NET GridView Using ObjectDataSource
Tuesday 5 August 2008 @ 4:57 pm

As a moderator in the asp.net 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" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>ODS DDL Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetMembers"
            TypeName="BusinessObject"></asp:ObjectDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
            DataSourceID="ObjectDataSource1">
            <Columns>
                <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">
                    <ItemTemplate>
                        <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>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 

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

 

x

Hope this helps!





5 Responses to “Assigning a DropDownList Value in a ASP.NET GridView Using ObjectDataSource”

  1. Greg Says:

    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!

  2. Peter Kellner Says:

    Greg,
    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.
    -Peter

  3. Tom Says:

    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

  4. John Says:

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

  5. Dante Says:

    http://msdn.microsoft.com/en-us/library/ms972948.aspx
    Replace original_ProductID with ProductID

Leave a Reply