Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#
Tip/Trick

How to have an Image for a Command Button in the kendo Grid?

Rate me:
Please Sign up or sign in to vote.
2.33/5 (6 votes)
11 Nov 2014CPOL1 min read 25.9K   2   2
This tip explains how we can have only image for any command button in the Kendo grid.

Introduction

While working with Kendo Grid in MVC, many times we come across a requirement when we want to have some images instead of default buttons of the grid.

I had a similar kind of requirement and as I was new to Kendo, it took me a lot of time to implement the same. After doing a lot of search and trying various methods for achieving the same, finally I got a perfect way to achieve this. I decided to write a post for this so that it can save time for others working with Kendo.

Below, I am going to explain how to add a delete image in my grid which will act as delete button of the grid.

Usually, we have a model class on which our kendo grid is based.

For example:

HTML
@(Html.Kendo().Grid<MyProject.Models.Mydata>()
                    .Name("Grid")
                    .Columns(columns =>
                        {
                           //columns of the grid here

                        })
                    .DataSource(dataSource => dataSource
                    .Ajax()
                    .Read(read => read.Action("BindGrid", "MyController"))
                        )) 

So we can see in the above example MyData is the class my grid is based on.

In this MyData class, along with the other required properties, we can add an extra property and name it as Delete.

HTML
public class MyData
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Delete { get; set; }
    }

Now, while defining columns in the grid, we can have one column for this Delete property too as we are going to have for Id and Name.

HTML
@(Html.Kendo().Grid<MyProject.Models.Mydata>()
   .Name("Grid")
   .Columns(columns =>
     {
       columns.Bound(p => p.Id);
       columns.Bound(p => p.Name);
       columns.Bound(p => p.Delete).ClientTemplate
       ("<img src='delete.png'onclick='deleteRow(this)' />");
     })

   .DataSource(dataSource => dataSource
   .Ajax()
   .Read(read => read.Action("BindGrid", "MyController"))  
 ))

As shown in the above code, for the Delete column, we have an image (delete.png).

This image will create an image button type of column in the grid.

We can have an onclick event for this image where we can implement the functionality we want for our custom button.

Hope it helps. :-)

License

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


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

Comments and Discussions

 
GeneralMy vote of 1 Pin
Jon Smith14-Nov-14 4:27
Jon Smith14-Nov-14 4:27 
GeneralRe: My vote of 1 Pin
Shivani Jha25-Nov-14 17:20
Shivani Jha25-Nov-14 17:20 

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.