Ternary Operator in GridView, DataList and Repeater

Introduction

In this article I will tell you how to use a Ternary Operator in GridView, DataList and Repeater.

As the name suggests a Ternary Operator contains three expressions. The ternary operator works on if and else conditions, if it finds the condition to be true then the first expression will be executed otherwise the second will be executed.

It has two main operators, ? and :, the "?" checks whether or not the condition is true and ":" separates the expressions to be executed.

It's syntax is as in the following:

condition ? first_expression : second_expression;

Now I will create an example to help you understand this syntax clearly.

Step 1

First of all I took a Grid View in ASP.NET application:

    <div>

        <asp:GridView ID="grd" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">

            <AlternatingRowStyle BackColor="White" />

            <EditRowStyle BackColor="#2461BF" />

            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

            <RowStyle BackColor="#EFF3FB" />

            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

            <SortedAscendingCellStyle BackColor="#F5F7FB" />

            <SortedAscendingHeaderStyle BackColor="#6D95E1" />

            <SortedDescendingCellStyle BackColor="#E9EBEF" />

            <SortedDescendingHeaderStyle BackColor="#4870BE" />

        </asp:GridView>

    </div>

Here I took a Grid View and some CSS is applied to it.

Step 2

Now I will bind this Grid with some data present in my database, for that you need to write this code in the .aspx file or in code behind:

        SqlConnection con = new SqlConnection();

        SqlCommand cmd = new SqlCommand();

        protected void Page_Load(object sender, EventArgs e)

        {

            con = new SqlConnection(@"Data Source=MCNDESKTOP20\MCNDESKTOP20;
            Initial Catalog=Student;Integrated Security=True"
);

            cmd.Connection = con;

            cmd.CommandText = "Select * from IT_Student";

            con.Open();

            grd.DataSource = cmd.ExecuteReader();

            grd.DataBind();

            con.Close();

        }

I bound this Grid with a database named "Student", in that database a table is present named "IT_Student".

Now if I my application is run then output like this will be seen:

ternary operator

You can see that some data is available in the grid, in the Name column all the cells are not filled in because I did not provide all the names of the user.

Step 3

Now I will work on the aspx.cs or design file and will show how the ternary operator can be applied.

Replace your previous Grid View code with this one:

    <div>

        <asp:GridView ID="grd" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">

            <AlternatingRowStyle BackColor="White" />

            <EditRowStyle BackColor="#2461BF" />

            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

            <RowStyle BackColor="#EFF3FB" />

            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

            <SortedAscendingCellStyle BackColor="#F5F7FB" />

            <SortedAscendingHeaderStyle BackColor="#6D95E1" />

            <SortedDescendingCellStyle BackColor="#E9EBEF" />

            <SortedDescendingHeaderStyle BackColor="#4870BE" />

            <Columns>

                <asp:TemplateField HeaderText="Testing">

                    <ItemTemplate>

                        <%#DataBinder.Eval(Container.DataItem,"Student_Name").ToString()==""?
                       
DataBinder.Eval(Container.DataItem,"Student_Name")+"No Name Provided":
                       
DataBinder.Eval(Container.DataItem,"Student_Name")+"Chaudhary" %>

                    </ItemTemplate>

                </asp:TemplateField>

            </Columns>

        </asp:GridView>

    </div>

Here I used the "Ternary Operator" in the <ItemTemplate> field.

Here first I created a column that will be shown as the first column of the Grid, in this column is a Template Field and then the Item template is used.

In the <ItemTemplate> I checked whether or not "Student_Name" is empty, if it's found to be empty then "No Name Provided" will be shown otherwise the name is shown along with the surname "Chaudhary".

Now our application is created and is ready for execution.

Ouptut

On running the application you will get output like this:

ternary operator

You can see that the rows without a Student Name are displaying "No Name Provided" and the others are shown with the surname "Chaudhary".

Up Next
    Ebook Download
    View all
    Learn
    View all