Tuesday, 30 July 2013

Make the color difference for Values in grid based on positive and negitive


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

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=;Database=;User Id=sa;password=;");
            SqlCommand cmd = new SqlCommand("Select * from Employee", conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            gdv.DataSource = dr;
            gdv.DataBind();
        }

        protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
                if (Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "empsal").ToString()) < 0)
                    e.Row.Cells[e.Row.Cells.Count - 1].ForeColor = System.Drawing.Color.Red;
                else
                    e.Row.Cells[e.Row.Cells.Count - 1].ForeColor = System.Drawing.Color.Green;
        }
    }
}

No comments:

Post a Comment