Displaying Image In WebGrid And Changing Background Color Of Webgrid Row On Mouse Hover

In one of my articles, I have described how to perform CRUD operation with WebGrid, using WebApI. You can check this in the following Link.
 
In this article, I will discuss on the following topics. 
  • Binding the images in the WebGrid.
  • Changing background color of the WebGrid rows on mouse hover on it.

    First of all, I will describe how to bind the images in the WebGrid. For this, we will have to create a new MVC Project. I have created one new MVC project, where I have a Home Controller and a model as follows:
    1. public class Employees  
    2.     {  
    3.         public int EmpId { get; set; }  
    4.         public string EmpName { get; set; }  
    5.         public string CompanyName { get; set; }  
    6.         public string Location { get; set; }  
    7.         public string Dept { get; set; }  
    8.         public string ContactNo { get; set; }  
    9.         public string Address { get; set; }  
    10.         public string ImageUrl { get; set; }  
    11.        
    12.     }  
    Now, I will  create an Action method to return the data to the view as follows:
    1. public async Task<ActionResult> Details()  
    2.        {  
    3.            List<Employees> li=new List<Employees>();  
    4.                 li.Add(new Employees{EmpId=1,Dept="IT",EmpName="Rajesh",Location="Bangalore",CompanyName="TCS",ImageUrl="~/IMAGES/10.jpg"});  
    5.                 li.Add(new Employees { EmpId = 2, Dept = "IT", EmpName = "Sumit", Location = "Bangalore", CompanyName = "CGI",ImageUrl="~/IMAGES/8.jpg" });  
    6.                 li.Add(new Employees { EmpId = 3, Dept = "IT", EmpName = "Archana", Location = "Bangalore", CompanyName = "TCS", ImageUrl = "~/IMAGES/5.jpg" });  
    7.                 li.Add(new Employees { EmpId = 4, Dept = "Sales", EmpName = "Kishor", Location = "Delhi", CompanyName = "IBM", ImageUrl = "~/IMAGES/3.jpg" });  
    8.                 li.Add(new Employees { EmpId = 5, Dept = "IT", EmpName = "Manoj", Location = "Bhubaneshwar", CompanyName = "IBM", ImageUrl = "~/IMAGES/4.jpg" });  
    9.   
    10.                 li.Add(new Employees { EmpId = 6, Dept = "IT", EmpName = "Pankaj", Location = "Chennai", CompanyName = "SLK", ImageUrl = "~/IMAGES/5.jpg" });  
    11.                 li.Add(new Employees { EmpId = 7, Dept = "IT", EmpName = "Satya", Location = "Bangalore", CompanyName = "SLK", ImageUrl = "~/IMAGES/6.jpg" });  
    12.   
    13.                 li.Add(new Employees { EmpId = 8, Dept = "IT", EmpName = "Ranjan", Location = "Delhi", CompanyName = "INFOSYS", ImageUrl = "~/IMAGES/7.jpg" });  
    14.                 li.Add(new Employees { EmpId = 9, Dept = "HR", EmpName = "Nabin", Location = "Bangalore", CompanyName = "TCS", ImageUrl = "~/IMAGES/8.jpg" });  
    15.   
    16.                 li.Add(new Employees { EmpId = 10, Dept = "Sales", EmpName = "sujit", Location = "chennai", CompanyName = "INFOSYS", ImageUrl = "~/IMAGES/3.jpg" });  
    17.                 li.Add(new Employees { EmpId = 11, Dept = "HR", EmpName = "prakash", Location = "chennai", CompanyName = "INFOSYS", ImageUrl = "~/IMAGES/10.jpg" });  
    18.              
    19.            return View(li); 
    Here, I have created a folder, where I have put my images, as given below:



    Now, here is my view to display the data in the WebGrid.
    1. @model IEnumerable<MVCProject.Models.Employees>
    2. @{
      ViewBag.Title = "Details";
      }
      <script src="~/Scripts/jquery-1.10.2.min.js"></script>

    3. <div>  
    4.     @{  
    5.     var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 10);  
    6.     grid.Pager(WebGridPagerModes.All);  
    7. }  
    8.   
    9. </div>  
    10.   
    11. <h3>List Of Users</h3>  
    12. <div>  
    13.     @grid.GetHtml(  
    14.                    headerStyle: "webgrid-header",  
    15.                 footerStyle: "webgrid-footer",  
    16.                 alternatingRowStyle: "webgrid-alternating-row",  
    17.                 selectedRowStyle: "webgrid-selected-row",  
    18.                 rowStyle: "webgrid-row-style",  
    19.                 mode: WebGridPagerModes.All,  
    20.              columns: grid.Columns(  
    21.   
    22.               grid.Column(columnName: "EmpId", header: "EmpId"),  
    23.               grid.Column(  
    24.   
    25.                  format: (item) =>  
    26.                  {  
    27.                      return Html.Raw(string.Format("<text><img src=\"{0}\" alt=\"Image\" width='100px' height='50px'/></text>", Url.Content(@item.ImageUrl)));  
    28.                  }  
    29.                 ),  
    30.   
    31.                                 grid.Column(columnName: "EmpName", header: "EmployeeName"),  
    32.   
    33.                                                        grid.Column(columnName: "Dept", header: "Department"),  
    34.   
    35.                                                            grid.Column(columnName: "CompanyName", header: "CompanyName"),  
    36.   
    37.                   grid.Column(columnName: "Location", header: "Location")));  
    38.      
    39.   
    40.    </div>  
    It is my jQuery Script and CSS.
    1. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
    2. <script src="~/Scripts/bootstrap.min.js"></script>  
    3.   
    4.     <style type="text/css" >  
    5.          .webgrid-table {  
    6.         font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;  
    7.         font-size: 1.2em;  
    8.         width: 300px;  
    9.         display: table;  
    10.         border-collapse: separate;  
    11.         border: solid 1px;  
    12.         background-color: purple;  
    13.     }  
    14.   
    15.     .webgrid-table td, th {  
    16.         border: 3px solid;  
    17.         padding: 3px 7px 2px;  
    18.         width: 230px;  
    19.     }  
    20.   
    21.     .webgrid-header {  
    22.         background-color:black;  
    23.         color:white;  
    24.         padding-bottom: 4px;  
    25.         padding-top: 5px;  
    26.         text-align: left;  
    27.         width: 30%;  
    28.         text-decoration-color:white;  
    29.     }  
    30.   
    31.     .webgrid-footer {  
    32.         background-color:black;  
    33.         height:50px;  
    34.     }  
    35.   
    36.     .webgrid-row-style {  
    37.         padding: 3px 7px 2px;  
    38.     }  
    39.   
    40.         .webgrid-alternating-row {  
    41.             background-color: #FFEBCD;  
    42.             padding: 3px 7px 2px;  
    43.         }  
    44. </script>
    Now, save the project and run it.This will give the following output:



    Now, my second aim is to change the background color of the row on which we will hover the mouse. For this, add the following CSS style, as given below:
    1. <style type="text/css" >
    2. .changeBackground {  
    3.         cursor: pointer;  
    4.         background:#00FF7F;  
    5.     }  
    6. </style>  
    7.   
    8. <script type="text/javascript">  
    9.     $(function () {  
    10.         $('tbody tr').hover(function () {  
    11.             $(this).toggleClass('changeBackground');  
    12.         })  
    13.     });  
    14. </script>   
    Now save the project and you will see the output.



    Thus, in this way, we can bind the image in the WebGrid and can change the backgroung color of the Webgrid row on which we hover the mouse. I hope this tutorial will help you to understand this for the initial stage.


Similar Articles