In this article I will explain with an example, how to use Ternary operator (? :) in ASP.Net controls such as GridView, DetailsView, DataList, Repeater, etc. using C# and VB.Net.
The Ternary operator (? :) will be used inside TemplateField (ItemTemplate) column along with EVAL function in ASP.Net using C# and VB.Net.
 
 
Concept
In database tables, we have some specific codes which have specific meaning. Programmers are aware of the significance of these codes but end users won’t understand what a particular code means. In such case we will have to dynamically change the code value to some meaningful and easy to understand words.
For example, consider a scenario of Meeting attendance. The Status column in database consists of two values A and P where A means Absent i.e. the person has not attended meeting and P means Present i.e. the person has attended the meeting.
Now end users won’t understand what is A and P and hence by making use of IF ELSE condition with EVAL function, A will be replaced with Absent and P will be replaced with Present which now makes it more meaningful.
 
 
Using Ternary operator in ASP.Net GridView
The following HTML markup consists of an ASP.Net GridView consisting of two BoundField columns and one TemplateField column populated using EVAL function.
Using Inline Expression, the value of the EVAL function is compared and if the Status value is A then the string Absent is displayed else Present.
Note: The syntax of using EVAL function within Inline Expression is different for C# and VB.Net.
 
C#
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="50" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:TemplateField HeaderText="Status" ItemStyle-Width="100">
            <ItemTemplate>
                <asp:Label Text='<%# Eval("Status").ToString() == "A" ? "Absent" : "Present" %>'
                    runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
VB.Net
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="50" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:TemplateField HeaderText="Status" ItemStyle-Width="100">
            <ItemTemplate>
                <asp:Label Text='<%# If(Eval("Status").ToString() = "A", "Absent", "Present") %>'
                    runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
 
Note: If the conditions are more complex, it is recommended to use RowDataBound event. For more details refer my article, Dynamically change GridView Cell value using RowDataBound event in ASP.Net using C# and VB.Net.
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Binding the ASP.Net GridView control
The GridView is populated with a dynamic DataTable with some dummy data inside the Page Load event.
Note: You can learn more about this dynamic DataTable in my article Create DataTable dynamically and bind to GridView in ASP.Net
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Status") });
        dt.Rows.Add(1, "John Hammond", "A");
        dt.Rows.Add(2, "Mudassar Khan", "P");
        dt.Rows.Add(3, "Suzanne Mathews", "P");
        dt.Rows.Add(4, "Robert Schidner", "A");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Status")})
        dt.Rows.Add(1, "John Hammond", "A")
        dt.Rows.Add(2, "Mudassar Khan", "P")
        dt.Rows.Add(3, "Suzanne Mathews", "P")
        dt.Rows.Add(4, "Robert Schidner", "A")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
 
Screenshot
Using Ternary operator with GridView, Repeater, DataList, DetailsView, FormView in ASP.Net
 
 
Demo
 
 
Downloads