Click here to Skip to main content
15,887,135 members
Articles / Web Development / ASP.NET
Tip/Trick

WebGrid: Cell color coding

Rate me:
Please Sign up or sign in to vote.
3.57/5 (5 votes)
26 Jun 2016CPOL1 min read 12.6K   2   2
Color code cells of WebGrid based on values of the cell

Introduction

Here we will learn how to apply color coding to cell(s) of a WebGrid, based on the cell values and conditions.

Background

I used VS 2013 Professional and MVC 5.

Using the code

This tip assumes that you already know how to create a MVC project. 

For this tip, I am going to take the example of certification scores and score percent. I am going to create a Dashboard view, which shows the details of certifications given by people. The view is will show details like Name of the certification giver, certification name, score and percent etc.,

Let us assume the pass percent for certifacate as 90 and the requirement is to color code the cell representing score percent.

Here is the condition based on which the color is determined:

1) Score Percent >= 90 then GREEN 

2) Else RED

This is the model I used:

C#
    public class CertificateModel
    {
        public string PersonName { get; set; }
        public string CertificateName { get; set; }
        public float MaxScore { get; set; }
        public float Score { get; set; }
        public float Percent { get; set; }
    }

Create a new Controller and name it CertificationController. Add a view for the Index controller action method of the controller and add code for displaying data in WebGrid. The view is a strongly typed view with CertificationModel as model.

Here is the code for the view:

C#
@model IEnumerable<MVCApp.Models.CertificateModel>

<h2>Certification Dashboard</h2>
@{
    ViewBag.Title = "Dashboard";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@{
    var certGrid = new WebGrid(Model, rowsPerPage: 5);
}

@certGrid.GetHtml(
tableStyle: "webGrid", headerStyle: "webGrid-header", 
htmlAttributes: new { name = "dash" }, alternatingRowStyle: "webGrid-altColor",
    columns: certGrid.Columns
    (
                certGrid.Column("PersonName", "Name", canSort: false),
                certGrid.Column("CertificateName", "Certificate Name", canSort: false),
                certGrid.Column("MaxScore", "Maximum Possible Score", canSort: false),
                certGrid.Column("Score", "Score Achieved", canSort: false),
                certGrid.Column("Percent", "Score %", canSort: false)
    )
)

Here is the code for Index controller action method code:

C#
public ActionResult Index()
{
    return View(certRepository.GetCertificates());
}

Image 1

Now let us add jQuery code for color coding. Here is that piece of code.

C#
 $(document).ready(function() {
        jQuery.each($("table[name='dash'] tbody tr td:nth-child(5)"), function () {
            if (parseFloat(this.textContent) >= 90) {
                $(this).css("background-color", "limegreen");
            }
            else if (parseFloat(this.textContent) < 90) {
                $(this).css("background-color", "red");
            }
        });
    });

Note: This code needs to be placed either in the _Layout.cshtml or in an external .js file. If you place this in the view we created for Dashboard it will not work, as the document will not be ready at that time.

Image 2

History

Intial Version - 06/26/2016

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to apply same in Excelsheet cell color coding using C# Pin
Abu Aamir Ahmad23-May-19 11:59
Abu Aamir Ahmad23-May-19 11:59 
GeneralMy vote of 3 Pin
Muhammad Shahid Farooq26-Jun-16 22:30
professionalMuhammad Shahid Farooq26-Jun-16 22:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.