Change Grid Size Dynamically Based on Screen Resolution

Introduction

 
This article explains how to change Grid Size dynamically based on screen resolution.
 
A condition is often put before the developers that the Grid size should change depending on the Screen Resolution of the user so that the user can easily access the Website at any Screen Resolution. Today I will show you something by which this problem will be solved and that too is in a simpler way.
 
Let's see the procedure required to create such an application.
 
Step 1
 
First of all I had worked on a webpage on which a JavaScript ready function is created in which the height will be sent to the WebService method of a WebService. It's code is as follows:
  1. <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">  
  2.      <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>  
  3.      <script language="javascript" type="text/javascript">  
  4.      $(document).ready(function() {  
  5.           var windowHeight = $(window).height();  
  6.           UIService.SaveNewHeight(windowHeight, OnCompleted);  
  7.      });  
  8.   
  9.      function OnCompleted() {}  
  10.      </script>  
  11. </asp:Content>  
  12. <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">  
  13.      <asp:ScriptManager ID="ScriptManager1" runat="server">  
  14.           <Services>  
  15.                <asp:ServiceReference Path="~/UIService.asmx" />  
  16.           </Services>  
  17.      </asp:ScriptManager> 
Here you can see that after creating the Ready function I had passed the reference of a WebService to which the WebService method was passed.
 
Step 2
 
The WebService method will be used to store the window height in a Session Variable, but first check whether your project has a WebService, if not then right-click on your WebSite/Poject and choose to "Add New Item".
 
Dynamic Grid Size1.jpg
 
Now in this WebService, the Webservice method will be used to pass the window height in a Session variable.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6. using System.Web.Script.Services;  
  7.    
  8. [WebService(Namespace = "http://tempuri.org/")]  
  9. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  10. [System.Web.Script.Services.ScriptService]  
  11. public class UIService : System.Web.Services.WebService {  
  12.    
  13.     public UIService () {  
  14.     }  
  15.    
  16.     [WebMethod(EnableSession = true)]  
  17.     [ScriptMethod()]  
  18.     public void SaveNewHeight(string height)  
  19.     {  
  20.         HttpContext.Current.Session["WindowHeight"] = height;  
  21.     }  

Step 3
 
Now I will add one more page to our application with a Grid View in it. Write this code on this new Web Page:
  1. <div style="height: 50Px">  
  2.     <asp:Label ID="showMessage" runat="server" Text="Label"></asp:Label>             
  3. </div>  
  4. <div id="GridView">  
  5.     <asp:GridView ID="GridView1" AutoGenerateColumns="true" PageSize="50" runat="server"  
  6.         AllowPaging="true" OnPageIndexChanging="GridView1_PageIndexChanging">  
  7.         <RowStyle Height="20px" />  
  8.     </asp:GridView>  
  9. </div> 
Here I had declared a Label and a GridView; the label will be used to show the Page and Grid Size.
 
Step 4
 
Now we will work on the code behind of this page, write this code in the code behind:
  1. int gridSize = 0;  
  2. int gridTop = 100;  
  3. int gridBottom = 50;  
  4. DataTable dt;  
  5. protected void Page_Load(object sender, EventArgs e)  
  6. {  
  7.     if (Session["WindowHeight"] != null)  
  8.     {  
  9.         gridSize = Convert.ToInt32((Convert.ToInt32(Session["WindowHeight"]) - gridTop - gridBottom) / 20) - 3;  
  10.         showMessage.Text = "Grid and Page Size Will be:" + gridSize.ToString();  
  11.     }  
  12.     else  
  13.     {  
  14.         gridSize = 20;  
  15.     }  
  16.   
  17.     if (!IsPostBack)  
  18.     {  
  19.   
  20.         GridView1.PageSize = gridSize;  
  21.         dt = UIHelper.GetData(300);  
  22.         GridView1.DataSource = dt;  
  23.         GridView1.DataBind();  
  24.     }  
  25. }  
  26.   
  27. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
  28. {  
  29.     GridView1.PageIndex = e.NewPageIndex;  
  30.     GridView1.PageSize = gridSize;  
  31.     GridView1.DataSource = UIHelper.GetData(300); ;  
  32.     GridView1.DataBind();  

Here first I had calculated the window and then the page size is set depending on the window size.
 
At the end coding for the page, index changing is done, this will be relevant when the user will click on the next page.
 
Until now our application is created and is ready to be executed.
 
Output
 
At the start, my Screen Resolution is 1920 * 1080.
 
Dynamic Grid Size2.jpg
 
So, on running the application I got the Grid with a page size of 38.
 
Dynamic Grid Size3.jpg
 
But now I am changing the Screen Resolution to 1600 * 900.
 
Dynamic Grid Size4.jpg
 
So, on running the application I got a Grid of page Size 29.
 
Dynamic Grid Size5.jpg