Responsive GridView Using Footable

In this article, we will see how to make an ASP GridView responsive using jQuery based Fooable plugin.

Introduction

Most of us are aware of what a responsive website is. A responsive website is a website that is compatible with different devices having different screen resolutions like desktop, laptop, tablet, mobile, etc.

The website is adapted to have best viewing experience in all the above devices without any horizontal scrollings and all. This is achieved mainly with the help of CSS, HTML 5, and jQuery.

There are a lot of plugins available for making the websites responsive and more attractive. One such plugin is Footable. 
It is mainly used to make table elements responsive. Before reading this article, I suggest you read my previous article which explains how to make an HTML table responsive using Footable.
Now, here, we are going to make the ASP GridView control responsive.

GridView is the most commonly used way of databinding in ASP.NET websites and in this modern era of responsive websites, it has become a tough task for many developers to migrate from GridView to new responsive tables.

So here, I'm explaining a simple way to make the same GridView responsive without many changes in our code! The main advantage of this method is that this can be implemented in already integrated GridViews too!

The basic trick behind this is that the ASP GridView is rendered as an HTML table in browsers!!

First, we will start with binding an ASP GridView. Here is our sample GridView containing the data of Employees table.
  1. <asp:GridView ID="grdviewnew" runat="server" CellPadding="5" CellSpacing="0" OnRowDataBound="grd_RowDataBound"  
  2.     AutoGenerateColumns="false" CssClass="myclass" Width="100%">  
  3.     <Columns>  
  4.         <asp:TemplateField HeaderText="Employee Code" HeaderStyle-HorizontalAlign="Center">  
  5.             <ItemTemplate>  
  6.                 <%#Eval("Employee Code")%>  
  7.             </ItemTemplate>  
  8.         </asp:TemplateField>  
  9.         <asp:TemplateField ItemStyle-HorizontalAlign="Left" HeaderText="Employee Name" HeaderStyle-HorizontalAlign="Center">  
  10.             <ItemTemplate>  
  11.                 <%#Eval("Employee Name")%>  
  12.             </ItemTemplate>  
  13.         </asp:TemplateField>  
  14.         <asp:TemplateField ItemStyle-HorizontalAlign="Left" HeaderText="Employee Age" HeaderStyle-HorizontalAlign="Center">  
  15.             <ItemTemplate>  
  16.                 <%#Eval("Employee Age")%>  
  17.             </ItemTemplate>  
  18.         </asp:TemplateField>  
  19.         <asp:TemplateField ItemStyle-HorizontalAlign="Left" HeaderText="Designation" HeaderStyle-HorizontalAlign="Center">  
  20.             <ItemTemplate>  
  21.                 <%#Eval("Designation")%>  
  22.             </ItemTemplate>  
  23.         </asp:TemplateField>  
  24.         <asp:TemplateField ItemStyle-HorizontalAlign="Left" HeaderText="Experience" HeaderStyle-HorizontalAlign="Center">  
  25.             <ItemTemplate>  
  26.                 <%#Eval("Experience")%>  
  27.             </ItemTemplate>  
  28.         </asp:TemplateField>  
  29.     </Columns>  
  30. </asp:GridView>  
Now, we are binding this GridView from the back-end by creating a datatable.
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!IsPostBack)  
  4.     {  
  5.         BindGrid();  
  6.     }  
  7. }  
  8.   
  9. public void BindGrid()  
  10. {  
  11.     DataTable dt = new DataTable();  
  12.     dt.Columns.Add("Employee Code");  
  13.     dt.Columns.Add("Employee Name");  
  14.     dt.Columns.Add("Employee Age");  
  15.     dt.Columns.Add("Designation");  
  16.     dt.Columns.Add("Experience");  
  17.     DataRow dru = dt.NewRow();  
  18.     dru["Employee Code"] = "10011";  
  19.     dru["Employee Name"] = "Rajeev";  
  20.     dru["Employee Age"] = "31";  
  21.     dru["Designation"] = "Developer";  
  22.     dru["Experience"] = "6";  
  23.     dt.Rows.Add(dru);  
  24.     dru = dt.NewRow();  
  25.     dru["Employee Code"] = "10012";  
  26.     dru["Employee Name"] = "Sandhya";  
  27.     dru["Employee Age"] = "27";  
  28.     dru["Designation"] = "Tester";  
  29.     dru["Experience"] = "2";  
  30.     dt.Rows.Add(dru);  
  31.     dru = dt.NewRow();  
  32.     dru["Employee Code"] = "10013";  
  33.     dru["Employee Name"] = "Ramesh";  
  34.     dru["Employee Age"] = "25";  
  35.     dru["Designation"] = "Designer";  
  36.     dru["Experience"] = "1";  
  37.     dt.Rows.Add(dru);  
  38.     dru = dt.NewRow();  
  39.     dru["Employee Code"] = "10014";  
  40.     dru["Employee Name"] = "Sanjay";  
  41.     dru["Employee Age"] = "32";  
  42.     dru["Designation"] = "Developer";  
  43.     dru["Experience"] = "5";  
  44.     dt.Rows.Add(dru);  
  45.     dru = dt.NewRow();  
  46.     dru["Employee Code"] = "10015";  
  47.     dru["Employee Name"] = "Ramya";  
  48.     dru["Employee Age"] = "23";  
  49.     dru["Designation"] = "Developer";  
  50.     dru["Experience"] = "1";  
  51.     dt.Rows.Add(dru);  
  52.     dt.TableName = "table1";  
  53.     grdviewnew.DataSource = dt;  
  54.     grdviewnew.DataBind();  
  55.  }  
Now, viewing this page in browser, we can see as below.


We can see that the above page won't fit in mobile screens and all columns in the table can't be seen without horizontal scrolling in mobile devices.



Now, we are going to make this GridView fit in all devices.

For this, first, we add the CSS and jQuery reference for FooTable plugin in the head section of our page, as below.
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head id="Head1" runat="server">  
  4.     <meta charset="utf-8">  
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">  
  6.     <meta name="viewport" content="width=device-width, initial-scale=1">  
  7.     <title>Responsive GridView using Footable</title>  
  8.     <link href="footable.core.css" rel="stylesheet" type="text/css" />  
  9.     <script src="jquery-1.10.2.min.js" type="text/javascript"></script>  
  10.     <script src="footable.js" type="text/javascript"></script>  
  11. </head>  
Note the use of meta tag with the name "viewport" in the head section. It is mandatory for the site to be responsive in small devices. If you have read my previous article specified above, you will know about data-hide attributes of FooTable.

The "data-hide" attribute is used to specify which columns have to be hidden in which resolution. The attribute is defined in the table header column with breakpoint names. For a normal table, we can give data-hide attributes like below.
  1. <thead>  
  2.      <tr>  
  3.          <th>  
  4.              Employee Code  
  5.          </th>  
  6.          <th>  
  7.              Employee Name  
  8.          </th>  
  9.          <th data-hide="phone">  
  10.              Employee Age  
  11.          </th>  
  12.          <th data-hide="phone,tablet">  
  13.              Designation  
  14.          </th>  
  15.          <th data-hide="phone,tablet">  
  16.              Experience  
  17.          </th>  
  18.      </tr>  
  19.  </thead>  
Here, phone & tablet are breakpoints given in FooTable plugin's JS file. We can edit those breakpoint names and values or add new breakpoints as we like.
  1. (function ($, w, undefined) {  
  2.     w.footable = {  
  3.         options: {  
  4.             delay: 100, // The number of millseconds to wait before triggering the react event  
  5.             breakpoints: { // The different screen resolution breakpoints  
  6.                 phone: 480,  
  7.                 tablet: 720  
  8.             },  
In the above script from Footable JS file, we can clearly see the value assigned to the breakpoints - phone and tablet. It refers to the width of the screen in pixels at which the columns with the above values in data-hide attribute will be hidden.

Here, if we want to hide a column in both the breakpoints, then we have to specify both those names in data-hide attribute with comma separation (phone, tablet).

But how to integrate this in ASP GridView as there is no thead or <th> in GridView?

So, here goes the most important part of this article. The changes we have to make to get our GridView responsive.
  1. grdviewnew.DataSource = dt;  
  2. grdviewnew.DataBind();  
  3. grdviewnew.HeaderRow.TableSection = TableRowSection.TableHeader;  
Note the highlighted line in the above code.

After the binding of GridView, we have to call this line of code. Basically, this code makes the GridView to group the header row inside a thead tag while rendering in browsers. Without this code, GridView renders the heading row inside <tbody> only.

So that overcomes our first problem of not having a thread for GridView.

The next issue is, giving data-hide attribute for header columns. For that, we have to write the below code inside Rowdatabound function of GridView.
  1. protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.         if (e.Row.RowType == DataControlRowType.Header)  
  6.         {  
  7.             TableCellCollection cell = e.Row.Cells;  
  8.             cell[2].Attributes.Add("data-hide""phone");  
  9.             cell[3].Attributes.Add("data-hide""tablet,phone");  
  10.             cell[4].Attributes.Add("data-hide""tablet,phone");  
  11.         }   
  12.     }  
  13.     catch (Exception ex)  
  14.     {  
  15.     }  
  16. }  
So, this will add the data-hide attribute to the corresponding header cells of GridView.

Now, we will initialize the FooTable property for this GridView.
  1. <script type="text/javascript">  
  2.     $(document).ready(function () {  
  3.         $('#<%=grdviewnew.ClientID%>').footable();  
  4.     });  
  5. </script>  
Now, running our page, we can see our GridView as below.

On desktop - No Change


On tablet - The last 2 columns are hidden.



On mobile - Last 3 columns are hidden.



Clicking/tapping on the Plus symbol, we can see the hidden column values.



That's it. Our ASP GridView is now responsive. We can view it on all the devices without any horizontal scrolling. This will work with the GridView having paging functionality also.

Reference

https://fooplugins.github.io/FooTable/index.html
 
Summary

This article covers the basic functionality of Footable plugin for making the ASP GridView responsive. There are a lot more functionalities available in Footable like sorting, paging, filtering, etc. You can go through the Footable site to explore more.

Hope this will be helpful!

Up Next
    Ebook Download
    View all
    Learn
    View all