Freezing gridview header and columns while scrolling rest of the content
Introduction
In asp.net forums I frequently find question about way to freeze gridview header and/or columns while allowing rest of content to scroll. For long gridview data it is also desirable from user perspective. Some of jQuery plugins are available to do this task. In this short article, I am going to demonstrate using "jQuery.FixedTable" plugin for asp.net gridview.Update: There was a issue with the alignment of frozen and normal columns. The same has been fixed and sample application is attached with this article. To know more read the update section towards the end of article.
Article Body
To create this sample, I have used "jQuery.FixedTable" plugin. You can download it here Also, I have used NorthWind database table to populate gridview in this sample.To start with, Follow below steps.
- Create new asp.net website
- Download and add jQuery.FixedTable.js file in your script folder of web site project
- Add reference to jQuery file jquery-1.4.1.min.js in aspx file
- Add reference to jQuery file jquery.fixedtable.js in aspx file
The jQuery plugin which we are going to use, works on simple table tag. The GridView control is rendered as normal table in the browser at the end. However, to use the plugin, we have to wrap GridView inside separate div control. The both Div and GridView controls should be given a unique class name which would then be used to bind it with the jQuery plugin.
The required aspx side code is
1
2
3
4
5
6
7
| < div id = "divGrid" class = "tableDiv" > < asp:GridView ID = "grdData" runat = "server" AutoGenerateColumns = "true" CellPadding = "4" GridLines = "None" class = "FixedTables" Width = "600" > </ asp:GridView > </ div > |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| <style type= "text/css" > body { width : 900px ; } p { float : left ; width : 100% ; margin : 20px 0px ; } .fixedColumn .fixedTable td { color : #FFFFFF ; background-color : #5097d1 ; font-size : 14px ; font-weight : normal ; } .fixedHead td, .fixedFoot td { color : #FFFFFF ; background-color : #5097d1 ; font-size : 14px ; font-weight : normal ; padding : 5px ; border : 1px solid #187BAF ; } .fixedTable td { font-size : 10pt ; background-color : #FFFFFF ; padding : 5px ; text-align : left ; border : 1px solid #CEE7FF ; } </style> |
1
2
3
4
| DataSet ds = cl.GetData( "select * from dbo.Customers" ); grdData.DataSource = ds; grdData.DataBind(); grdData.HeaderRow.TableSection = TableRowSection.TableHeader; |
Our jQuery plugiin also requires that header row of the HTML table should be enclosed inside <thead> tag. However, when rendered by default, GridView does not emit <thead> tag and hence, jQuery plugin fails to freeze the header row.
To make GridView control emit the <thead> tag, we can use HeaderRow.Tablesection property of gridview. It should be used only after DataBind has been called for GridView.
Using jQuery Plugin
After following all above steps, we would get simple GridView populated with data. Now, this is the time to associate it with the jQuery plugin and see freezing of gridview header and columns.
Add below javascript code in head of your page
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| <script type= "text/javascript" > $(document).ready( function () { $( ".tableDiv" ).each( function () { var Id = $( this ).get(0).id; var maintbheight = 355; var maintbwidth = 860; $( "#" + Id + " .FixedTables" ).fixedTable({ width: maintbwidth, height: maintbheight, fixedColumns: 1, // header style classHeader: "fixedHead" , // footer style classFooter: "fixedFoot" , // fixed column on the left classColumn: "fixedColumn" , // the width of fixed column on the left fixedColumnWidth: 100, // table's parent div's id outerId: Id, // tds' in content area default background color Contentbackcolor: "#FFFFFF" , // tds' in content area background color while hover. Contenthovercolor: "#99CCFF" , // tds' in fixed column default background color fixedColumnbackcolor: "#5097d1" , // tds' in fixed column background color while hover. fixedColumnhovercolor: "#99CCFF" }); }); }); $( ".fixedTable table" ).attr( 'rules' , 'All' ); </script> |
In FixedTable function in jQuery code above, you can change value of parameter named fixedColumns; so as to increase number of columns in freeze mode or remove column freezing (freeze headers only). the fixedColumnWidth parameter indicates total width of all columns in freeze mode. Hence, if you increase number of columns in freeze mode then, increase this width or if you remove column freezing (by setting fixedColumns parameter to 0) then set the width parameter to zero.
If you find any issue with this code or plugin, please let me know. I will try to fix the same.
I hope somebody out there find this short article useful. Thanks for visiting my site! :)
Update:
While following this article, many readers faced issue regarding alignment of frozen column with rest of columns and many of them reported in the comment and some even dropped me a e-mail requesting to fix the same. Although, there would not be any issue in case you are using IE. But, with FireFox the issue was quite visible.
It took me quite some time to spend some time on it. But, here is the fix.
simply add this line at the end of JavaScript code (The JavaScript code section above has been update with the same).
$(".fixedTable table").attr('rules', 'All');
I have tested this fix in multiple browsers. However if you still face the issue, Please drop a line the comment section. Additionally, I have also attached the sample demo application below.
GridFreeze.zip (134.22 kb)
Good article, example is working fine,Thanks
ReplyDeleteThis comment has been removed by the author.
ReplyDeletePin and Unpin gridview column to freeze the data grid.
ReplyDeleteGridview freeze and scroll ..example..Gridview freeze and scrol
ReplyDeleteling
pin and unpin gridview rows to freeze the header
ReplyDeleteThank you so much for this article it helped me a lot with my gridview. But, I have a concern where when i load the page for the very first time the Gridview comes up with Frozen header and columns which i want but once the page is sent to server and it reloads my Gridview goes to its previous form where the headers and first few columns are no longer frozen. Am i missing something???
ReplyDelete