Thursday, 24 October 2013

GRID VIEW GRATUITY HIGHLIGHT


.SQL


create database GRATUITY
USE GRATUITY


CREATE TABLE Tbl_EMPLOYEES
(
SrNo int identity(1,1),
name varchar(100),
address nvarchar(max),
empno varchar(10),
Joining_Date date
)

SELECT * FROM Tbl_EMPLOYEES

insert into Tbl_EMPLOYEES(name,address,empno,Joining_Date) values('VAMSI','Bangalore','SUD010','2011-08-31')


.ASPX

<form id="form1" runat="server">
    <div>
        <asp:GridView ID="GVUserDetails" runat="server" OnRowDataBound="GVUserDetails_OnRowDataBound">
        </asp:GridView>
        <br />
       <%-- <asp:Label ID="lblerrmsg" runat="server" ForeColor="Red"></asp:Label>--%>
        <br />
    </div>
    </form>

C#



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

public partial class EMP_GT : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=GRATUITY;Integrated Security=true;");
    SqlCommand cmd;
    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }

    }
    public void BindData()
    {
        try
        {
            con.Open();
            string str = "Select *, DateDiff(Year,Joining_Date,GETDATE()) as Eligible_For_Gratuity From Tbl_EMPLOYEES";
            cmd = new SqlCommand(str, con);
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(ds);

            if (ds.Tables[0].Columns.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    GVUserDetails.DataSource = ds;
                    GVUserDetails.DataBind();
                    con.Close();
                }
            }


        }
        catch (Exception ex)
        {
            lblerrmsg.Text = ex.Message;
            con.Close();
        }
    }
    protected void GVUserDetails_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            int years = Convert.ToInt32(e.Row.Cells[5].Text);
            if (years >= 5)
            {
                e.Row.BackColor = Color.Green;

                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[5].Text = "Yes";
                }
            }
            else if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[5].Text = "No";
            }

        }
        catch (Exception ex)
        {
            lblerrmsg.Text = ex.Message;
            con.Close();
        }
    }
}

OUT PUT:








No comments:

Post a Comment

Receive All Free Updates Via Facebook.