Sunday, August 11, 2019

How to freeze gridview column in asp.net using jquery

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok. 
In this example, I have used one tables as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >

Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish. 

Step-5: Add a Webpage and Design for Show Data in Fixed column Gridview.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

HTML Code 
  1. <h3>Freeze gridview column using jquery in asp.net</h3>
  2. <br />
  3. <div id="gridContainer">
  4. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="5"
  5. CssClass="myGrid" AlternatingRowStyle-CssClass="alt">
  6. <Columns>
  7. <asp:BoundField DataField="UserName" HeaderText="Username" ItemStyle-Width="120px" />
  8. <asp:BoundField DataField="FullName" HeaderText="Full Name" ItemStyle-Width="200px" />
  9. <asp:BoundField DataField="EmailID" HeaderText="EmailID" ItemStyle-Width="200px"/>
  10. <asp:BoundField DataField="IsActive" HeaderText="Is User Active?" ItemStyle-Width="200px"/>
  11. </Columns>
  12. </asp:GridView>
  13. </div>
CSS Code 
  1. <style>
  2. .myGrid {
  3. background-color: #fff;
  4. margin: 5px 0 10px 0;
  5. border: solid 1px #525252;
  6. border-collapse:collapse;
  7. }
  8. .myGrid td {
  9. padding: 2px;
  10. border: solid 1px #c1c1c1;
  11. color: #717171;
  12. }
  13. .myGrid th {
  14. padding: 4px 2px;
  15. color: #fff;
  16. background-color: #424242;
  17. border-left: solid 1px #525252;
  18. font-size: 0.9em;
  19. }
  20. .myGrid .alt {
  21. background-color: #EFEFEF;
  22. }
  23. </style>
JS Code 
  1. <script>
  2. $(document).ready(function () {
  3. // here clone our gridview first
  4. var tab = $("#<%=GridView1.ClientID%>").clone(true);
  5. // clone again for freeze
  6. var tabFreeze = $("#<%=GridView1.ClientID%>").clone(true);
  7.  
  8. // set width (for scroll)
  9. var totalWidth = $("#<%=GridView1.ClientID%>").outerWidth();
  10. var firstColWidth = $("#<%=GridView1.ClientID%> th:first-child").outerWidth();
  11. tabFreeze.width(firstColWidth);
  12. tab.width(totalWidth - firstColWidth);
  13.  
  14. // here make 2 table 1 for freeze column 2 for all remain column
  15.  
  16. tabFreeze.find("th:gt(0)").remove();
  17. tabFreeze.find("td:not(:first-child)").remove();
  18.  
  19. tab.find("th:first-child").remove();
  20. tab.find("td:first-child").remove();
  21.  
  22. // create a container for these 2 table and make 2nd table scrollable
  23. var container = $('<table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top"><div id="FCol"></div></td><td valign="top"><div id="Col" style="width:320px; overflow:auto"></div></td></tr></table)');
  24. $("#FCol", container).html($(tabFreeze));
  25. $("#Col", container).html($(tab));
  26.  
  27. // clear all html
  28. $("#gridContainer").html('');
  29. $("#gridContainer").append(container);
  30. });
  31. </script>

Step-6: Write below code in page_load event for fetch data from database and show in Gridview.


  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. // populate data here
  4. if (!IsPostBack)
  5. {
  6. //here MyDatabaseEntities is our dbcontext
  7. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  8. {
  9. GridView1.DataSource = dc.UsersDatas.ToList();
  10. GridView1.DataBind();
  11. }
  12. }
  13. }
Step-7: Run Application

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...