Saturday 19 January 2013

JQuery:Highlight GridView Negative value columns in ASP.NET

JQuery:Highlight GridView Negative value columns in ASP.NET
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>JQuery:Highlight GridView Negative value columns in ASP.NET </title>
    <style type="text/css">
        .Highlight

        {
            color: #FF008C;
            font-weight: bold;
            font-family: Verdana;
        }
    </style>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        ///for all columns
        $(document).ready(function () {
            $("#gvNegative td").each(function () {
                var celData = $(this).text();
                if ($.isNumeric(celData)) {
                    var numValue = parseInt(celData);
                    if (numValue < 0)
                        $(this).addClass('Highlight');
                }
            });
        });
        ///only for particular Column
        $(document).ready(function () {
            $("#gvNegative  tr:has(td)").each(function () {
                var $targetColumn = $(this).find("td:eq(1)"); /// set 2nd column
                var celData = $targetColumn.text();
                if ($.isNumeric(celData)) {
                    var numValue = parseInt(celData);
                    if (numValue < 0)
                        $targetColumn.addClass('Highlight');
                }
            });
        });
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvNegative" runat="server" BackColor="White" BorderColor="#CC9966"
            BorderStyle="Solid" BorderWidth="1px" CellPadding="4" Font-Names="Georgia" Font-Size="Small"
            Width="550px">
            <FooterStyle BackColor="Tan" />
            <AlternatingRowStyle BackColor="#E6E6E1" />
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>
In C#:
using System.Data;
using System.Data.SqlClient;


public partial class NegativeGridRow : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Product Name");
        dt.Columns.Add("Profit/Loss");
        dt.Rows.Add("Monitor", "-999");
        dt.Rows.Add("LCD", "200");
        dt.Rows.Add("LED", "999");
        dt.Rows.Add("Mouse", "-20");
        dt.Rows.Add("Processor", "1000");
        dt.Rows.Add("Wifi-Router", "-100");
        dt.Rows.Add("Hard Drive", "100");
        gvNegative.DataSource = dt;
        gvNegative.DataBind();
    }
}
In VB.NET:
Imports System.Data
Imports System.Data.SqlClient


Partial Public Class NegativeGridRow
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim dt As New DataTable()
        dt.Columns.Add("Product Name")
        dt.Columns.Add("Profit/Loss")
        dt.Rows.Add("Monitor", "-999")
        dt.Rows.Add("LCD", "200")
        dt.Rows.Add("LED", "999")
        dt.Rows.Add("Mouse", "-20")
        dt.Rows.Add("Processor", "1000")
        dt.Rows.Add("Wifi-Router", "-100")
        dt.Rows.Add("Hard Drive", "100")
        gvNegative.DataSource = dt
        gvNegative.DataBind()
    End Sub
End Class


output

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.