Pages

Wednesday, 26 June 2013

Fixed Header Scrollable GridView using JQuery


In this example I'm explaining how to create Fixed Header Scrollable Gridview Using jQuery in ASP.NET. add JQuery library and fixed header scrollable gridview plugin in solution and add reference to it between <head></head> section of HTML source of aspx page.
I have used northwind database to populate gridview.

you can also create Scrollable GridView With Fixed Headers Using CSS if you don't want to use jQuery or JavaScript.


 <head id="Head1" runat="server">
<title>jQuery Fixed Header Scrollable GridView</title>
<script src="jquery-1.4.1.min.js" type="text/javascript">
</script>
<script src="ScrollableGrid.js" type="text/javascript">
</script>
</head>

Add this JQuery function call between <head></head> section
<script type="text/javascript" language="javascript">$(document).ready(
function () {
$(
'#<%=fixedHeaderScrollableGridView.ClientID %>').Scrollable();
}
)
</script>

Add gridview on aspx page and populate it.

<asp:GridView ID="fixedHeaderScrollableGridView" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" DataKeyNames="ProductID" AllowPaging="True" PageSize="30">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" />
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" />
<asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [ProductID], [ProductName], [QuantityPerUnit], [UnitPrice], [CategoryName] FROM [Alphabetical list of products]"></
asp:SqlDataSource>

Build and Run the application.

If your application uses Master Page then add reference to library and plugin in head ContentPlaceHolder of master page


<head id="Head1" runat="server">

<title>Fixed Header Scrollable GridView With Master Page</title>
<asp:ContentPlaceHolder ID="head" runat="server">
<script src="jquery-1.4.1.min.js" type="text/javascript" />
<script src="ScrollableGrid.js" type="text/javascript" />
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form2" runat="server">
Add Master Page Content And Design Here
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</form>
</body>

Call Scrollable() function of jquery plugin in Head ContentPlaceHolderID of content page.

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<script type="text/javascript" language="javascript">
$(document).ready(
function () {
$(
'#<%=fixedHeaderScrollableGridView.ClientID %>').Scrollable();
})
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
Put Gridview Source Here
</div>
</asp:Content>

Scrollable GridView With Fixed Headers Using CSS in asp.net


Scrollable GridView With Fixed Headers Using CSS Example In Asp.Net 2.0,3.5,4.0 C# VB.NET. In this example I am going to show how to create scrollable GridView with fixed headers which don't get scrolled with records and stay on the top in asp.net using css, I've tested this code on IE8.


For this we need to add css to headers of gridview to keep them on the top.

First of all place a Panel on the aspx page from toolbox. Set height to 300px and width to 300px
and scrollbars to Vertical.

Now add a gridview inside this Panel and set the datasource to populate gridview.

We can also use JQuery to create Fixed Header Scrollable GridView.


HTML Source Code

<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server" Height="300px" Width="300px" ScrollBars="Vertical">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1" RowStyle-VerticalAlign="Bottom" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
</Columns>
<HeaderStyle CssClass="header" Height="20px" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [ID], [Name], [Location] FROM [Details]"></asp:SqlDataSource>
</asp:Panel>
</div>
</form>

Now Add below mention css style in the head section of page

<head id="Head1" runat="server">
<title>Creating scrollable GridView with fixed headers</title>
<style type="text/css">
.header
{
font-weight: bold;
position: absolute;
background-color: White;
}
</style>
</head>

Build and run the code.

This code works fine but there is one bug in it , first record of GridView gets hidden behind the fixed column headers.

To fix this issue we need to set height of first row in gridview to double of height of header row or double the height of other rows of gridview. for this we need to add below mentioned code in RowDataBound event of GridView.


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == 0)
e.Row.Style.Add("height", "40px");
}
}