Monday, August 22, 2016

Using Conditional Operator in GridView

In this article I will show how to use Conditional Operator in a GridView.

Conditional Operator ( ? : ) is a ternary operator which takes three operands.
Its syntax is
condition ? first expression : second expression

It works in a way that if the condition is true, it will evaluate the first expression and
if it is false, second expression is evaluated.

Consider an example in which a GridView is binded with User data and
it has a template field which displays User Type based on the integer value
stored in the database.

HTML Code (.aspx) :


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" 
BorderColor
="#E7E7FF"
BorderStyle
="None"
BorderWidth
="1px"
CellPadding
="3"
GridLines
="Horizontal">
            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <Columns>
                <asp:BoundField DataField="AGN_NAME" HeaderText="Name" SortExpression="AGN_NAME" />
                <asp:BoundField DataField="AGN_TEL" HeaderText="Telephone" SortExpression="AGN_TEL" />
                <asp:BoundField DataField="AGN_ID" HeaderText="User ID" SortExpression="AGN_ID" />
                <asp:TemplateField HeaderText="User Type" SortExpression="AGN_TYPE">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%#(DataBinder.Eval(Container, "DataItem.AGN_TYPE").ToString()=="1") ? "Active":"In active"%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <AlternatingRowStyle BackColor="#F7F7F7" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Here you can see in the template field if  the value of 
DataBinder.Eval(Container, "DataItem.AGN_TYPE") is 1, then output
will be displayed as "Active" else "In active".

C# Code (.aspx.cs) :


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class grid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            getData();
    }
    protected void getData()
    {
        DataSet ds = new DataSet();
        string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;

        using (SqlConnection dbConnection = new SqlConnection(connectionString))
        using (SqlCommand dbCommand = new SqlCommand())
        {
            dbCommand.CommandText = "select * from Agents";
            dbCommand.Connection = dbConnection;
            dbConnection.Open();

            SqlDataAdapter da = new SqlDataAdapter(dbCommand);
            da.Fill(ds);

        }
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}

Output :




No comments:

Post a Comment