Sunday, August 11, 2019

How to make Scrollable GridView with a Fixed Header (freeze row) in .NET

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 header 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>Scrollable Gridview with fixed header in ASP.NET</h3>
  2. <br />
  3. <div style="width:550px;">
  4. <div id="GHead"></div>
  5. <%-- This GHead is added for Store Gridview Header --%>
  6. <div style="height:300px; overflow:auto">
  7. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
  8. CellPadding="5" HeaderStyle-BackColor="#f3f3f3">
  9. <Columns>
  10. <asp:BoundField HeaderText="ID" DataField="StateID" />
  11. <asp:BoundField HeaderText="Country" DataField="Country" />
  12. <asp:BoundField HeaderText="StateName" DataField="StateName" />
  13. </Columns>
  14. </asp:GridView>
  15. </div>
  16. </div>
JS Code 
  1. <script src="Scripts/jquery-1.7.1.js"></script>
  2. <script language="javascript" >
  3. $(document).ready(function () {
  4. var gridHeader = $('#<%=GridView1.ClientID%>').clone(true); // Here Clone Copy of Gridview with style
  5. $(gridHeader).find("tr:gt(0)").remove(); // Here remove all rows except first row (header row)
  6. $('#<%=GridView1.ClientID%> tr th').each(function (i) {
  7. // Here Set Width of each th from gridview to new table(clone table) th
  8. $("th:nth-child(" + (i + 1) + ")", gridHeader).css('width', ($(this).width()).toString() + "px");
  9. });
  10. $("#GHead").append(gridHeader);
  11. $('#GHead').css('position', 'absolute');
  12. $('#GHead').css('top', $('#<%=GridView1.ClientID%>').offset().top);
  13. });
  14. </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. if (!IsPostBack)
  4. {
  5. PopulateData();
  6. }
  7. }
and here is the function

  1. private void PopulateData()
  2. {
  3. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  4. {
  5. GridView1.DataSource = dc.StateDatas.ToList();
  6. GridView1.DataBind();
  7. }
  8. }

And this function is also required to add. This is required to solve this problem --> Control 'MainContent_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

  1. public override void VerifyRenderingInServerForm(Control control)
  2. {
  3. // this is required for avoid error (control must be placed inside form tag)
  4. }

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 { ...