Skip to content

Assigning a Custom Label Value in a ASP.NET GridView Using ObjectDataSource

Updated: at 02:12 PM

In my previous post, Assigning a DropDownList Value in a ASP.NET GridView Using ObjectDataSource, I discussed how to put a DropDownList value in a GridView.  As I explained, my motivation was to answer a common question that appears on often on the forums.  If you are wondering what my process is for deciding what to answer, there really is no process besides I see yet another question on the forum about this, and rather than answer it again, I write a post explaining it, then point my answer to the question at the post.  This is what I did yesterday and below is the response to this forum post.


So, I have to say one of my main motivations for doing posts is I enjoy helping others through problems.  It gives me a certain satisfaction to know that I have knowledge others may not at any give time and I have a great way to share that knowledge.  It also makes me feel good when others go out of their way to appreciate the effort.  I know when I post questions on forums and get good answers how happy I am, it's good to know others appreciate my efforts.

So, continuing down that path, Here is the code necessary to answer Zimbran's last question.

<%@ 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" 
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                <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:Label ID="LabelYesOrNo" runat="server" 
                           Text='<%# (bool) Eval("Approved") ? "Yes" : "No"  %>'></asp:Label>
Check out the ORM (Object Relational Mapper) PRISMA. The database access method I use in all my projects