Applying styles to Gridview in ASP.Net using custom CSS (e.g, Twitter Bootstrap)

Gridview is one of the most powerful web controls which is used in most of the ASP.Net web applications to display information to the user in tabular form. In this article, we are going to discuss on

  1. How to apply styles to gridview
  2. How to apply style to a particular row based on the row values

Before discussing on how to apply styles in gridview, let me the explain the role of HTML,CSS in ASP.Net webform application. Understanding these basics would help you to handle the things better.

Role of HTML,CSS,Javascript in ASP.Net web application(or web application using any framework):

Web browsers (Internet Explorer,Firefox, Chrome etc..) are the applications which can understand only HTML, CSS and Javascript. Ofcourse, there are few exceptions. We can install some plugins in the browser so that it can interpret few other things as well. But most of the browsers (as it is installed) would understand only HTML, CSS and Javascript.   Irrespective of the web framework you are using to build the web application(ASP.Net, Java Struts2,Ruby on rails, Django etc..) your web application should be converted to HTML,CSS,Javascript  for the browser to understand. This is the reason you don’t need different browsers for the web applications built on different technologies as all are being converted to HTML,CSS and Javascript so that all browsers can understand.

HTML would provide you the layout of the web page – which content should be at what place

CSS(Cascading style sheet) would help you in applying styles to elements of the page. E.g, If you want all paragraph text to be italic and in blue color, CSS would help you to achieve it easily without much modification to the code. I am including inline styles as well in this category for clarity.

Javascript is for processing the elements at client side without going back to the server. Using ajax, though you can go to the server to get server data. Javascript is mainly used for validation, handling the user input, changing the style of some element based on data or user input etc…

So the web controls that you use in your webform would be converted to any of the HTML elements as shown in the below picture. For example, asp:Textbox would be converted to input HTML element, asp:Linkbutton would be converted to anchor link HTML(<a>) and so on… This conversion is required for all web controls as browsers can understand only HTML,CSS and Javascript.

ASP.Net Server Control HTML

Out topic of interest – Gridview web control in ASP.Net is converted to table element of HTML. So whatever the style you want to apply to the gridview would be applied to table element of the generated HTML.  We have had enough theory. Let’s apply some style to Gridview so that it can look great J

Applying style in Gridview:

You can apply your custom style for the Gridview as per your application style. In this article, we are going to use Bootstrap CSS. Bootstrap( is one of the popular front end framework which is being used in many of the web applications today. You can either download the related CSS,javascript to use in your application or you can use them directly by referring them at their CDN. In this example, I have referred the CSS and JS directly as shown below.

&lt;head runat=&quot;server&quot;&gt;


&lt;!-- Latest compiled and minified CSS --&gt;

&lt;link rel=&quot;stylesheet&quot;href=&quot;//;/&gt;

&lt;!-- Optional theme --&gt;

&lt;link rel=&quot;stylesheet&quot;href=&quot;//;/&gt;

&lt;!-- Latest compiled and minified JavaScript --&gt;

&lt;script src=&quot;//;&gt;&lt;/script&gt;


There is an attribute by name CssClass in Gridview. If you set this property to a style, it would impact the whole table. I am going to use bordered table style for my gridview as depicted in the link –

&lt;asp:GridViewID=&quot;gv&quot; runat=&quot;server&quot; CssClass=&quot;table table-hover&quot; AutoGenerateColumns=&quot;false&quot;   DataKeyNames=&quot;ProductNumber&quot;&gt;


&lt;asp:BoundField DataField=&quot;ProductNumber&quot; HeaderText=&quot;Product Number&quot;  /&gt;

&lt;asp:BoundField DataField=&quot;Name&quot; HeaderText=&quot;Name&quot; SortExpression=&quot;Name&quot;  /&gt;

&lt;asp:BoundField DataField=&quot;ReorderPoint&quot; HeaderText=&quot;ReorderPoint&quot; SortExpression=&quot;ReorderPoint&quot;  /&gt;

&lt;asp:BoundField DataField=&quot;ListPrice&quot;HeaderText=&quot;ListPrice&quot; SortExpression=&quot;ListPrice&quot;  /&gt;



You can combine multiple css classes by mentioning them separated by space. In the above gridview, I have used css classes table , table-hover


The resulting gridview is shown above. I am going to add table-bordered class to CssClassAttribute so that the table is bordered nicely. The complete CssClass attribute value is

CssClass=”table table-hover table-bordered”

And the resulting gridview is shown below

GridView_CSS_table bordered

Applying style to a particular row based on the data:

Let’s consider a scenario where we need to apply different style to a particular row based on row values so that it would be highlighted to the user. To achieve the same, define the event for OnRowDataBound and in this event apply the style for a particular row based on a value.  Using the same Product table of AdventureWorks database, I would like to apply style success if the ReOrderPoint is greater than 500. Else, I want to apply the danger style defined in bootstrap.

&lt;asp:GridViewID=&quot;gv&quot; runat=&quot;server&quot; CssClass=&quot;table table-hover table-bordered&quot; AutoGenerateColumns=&quot;false&quot; OnRowDataBound=&quot;gv_RowDataBound&quot;   DataKeyNames=&quot;ProductNumber&quot;&gt;


&lt;!—Columns omitted for clarity --&gt;



protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)


if (e.Row.RowType == DataControlRowType.DataRow)


int reorderPoint = Convert.ToInt32(e.Row.Cells[2].Text);

e.Row.CssClass = reorderPoint &gt; 500 ? &quot;success&quot; : &quot;danger&quot;;



In the above gridview RowDataBound event, I am applying success style to the row if the Reorderpoint column(3rd Column – as its zero based index I am taking value as e.Row.Cells[2].Text)  value is greater than 500. Else I am applying danger style.

Now, when you run the application, you would get gridview like below.


Please let me know your thoughts in the comments section.

Thanks for reading the article. Please subscribe to my mailing list below.







Leave a Reply

Your email address will not be published. Required fields are marked *