In this article I will explain how to make a resizable Grid View in ASP.NET using jQuery.

The following is my aspx code.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Resizable Gridview in ASP.NET using jQuery</title>
  6. <script src="jquery-1.8.2.js" type="text/javascript"></script>
  7. <link href="jquery-ui.css" rel="stylesheet" type="text/css" />
  8. <script src="jquery-ui.js" type="text/javascript"></script>
  9. <script type="text/javascript">
  10. $(function() {
  11. $("#gridDivResize").resizable();
  12. })
  13. </script>
  14. </head>
  15. <body>
  16. <form id="form1" runat="server">
  17. <div id="gridDivResize" class="ui-widget-content" style="width: 550px; padding: 2px">
  18. <asp:GridView ID="GridView1" CellPadding="4" runat="server" Width="100%" ForeColor="#333333"
  19. GridLines="None">
  20. <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  21. <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  22. <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  23. <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  24. <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  25. <EditRowStyle BackColor="#999999" />
  26. <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  27. </asp:GridView>
  28. </div>
  29. </form>
  30. </body>
  31. </html>
Here I am binding this grid view from one of my SQL Server data tables.

The following is my Data Table design.

table design view
Image 1

The data in my data table is: I am showing only CompanyName, Employee Name, Joining Date and Experience columns in GridView.

show data
Image 2

My aspx.cs code is:
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using System.Data.SqlClient;
  13. public partial class _Default : System.Web.UI.Page
  14. {
  15. SqlDataAdapter da;
  16. DataSet ds = new DataSet();
  17. protected void Page_Load(object sender, EventArgs e)
  18. {
  19. if (!IsPostBack)
  20. {
  21. BindGridviewData();
  22. }
  23. }
  24. protected void BindGridviewData()
  25. {
  26. SqlConnection con = new SqlConnection();
  27. con.ConnectionString = @"Data Source=MyPC\SqlServer2k8;Integrated Security=true;Initial Catalog=Test";
  28. SqlCommand cmd = new SqlCommand("SELECT CompanyName,EmployeeName,JoiningDate,Experience FROM EMPLOYEE", con);
  29. da = new SqlDataAdapter(cmd);
  30. da.Fill(ds);
  31. con.Open();
  32. cmd.ExecuteNonQuery();
  33. con.Close();
  34. if (!object.Equals(ds.Tables[0], null))
  35. {
  36. if (ds.Tables[0].Rows.Count > 0)
  37. {
  38. GridView1.DataSource = ds;
  39. GridView1.DataBind();
  40. }
  41. else
  42. {
  43. GridView1.DataSource = null;
  44. GridView1.DataBind();
  45. }
  46. }
  47. }
  48. }
Now run the application:

show record
Image 3

Now resize the Grid View.

expend the size of grid view
Image 4

show data record
Image 5