Click here to Skip to main content
Email Password   helpLost your password?

Introduction

I was developing a web based application that needed a datagrid/table that can freeze its header and columns, and also have a complex that consists of merged cells. I have searched on the internet but I still can't find the one that I wanted. So, I decided to build it by my self, and thank god I made it :). And now I want to share the code with the world. This article describe how to build a table with freeze capability using built-in ASP.Net controls and some HTML tags.

Using the code

To build the freeze-able table we need to use the DIV, TABLE tag, ASP.Net Panel and PlaceHolder control and of course some javascript and CSS. You can see the layout in Figure 1.

Figure 1. The layout to position the tag and controls.
Object Name Description
divScrollbar We will use this to show horizontal scrollbar. So we can show the hidden area of tblPart2 using JavaScript and give the impression that we freeze the columns in tblPart1. This will be accomplished using JavaScript.
ASP Panel control Optional. This will be used only if you want to be able to show or hide the entire objects.
divItems This DIV will be used for vertical scrollbar so we can give impression to the user that the header is frozen. We will do this using CSS.
TABLE tag To synchronize the position of tblPart1 and tblPart2.
tblPart1 This table will be container of the freezing columns. We will generate the rows later.
divPart2 This DIV is use to clip the view of tblPart2
tblPart2 This table is like tblPart1, but some of it's columns is hidden by divPart2. This table will be scrolled horizontally by divScroller. The header and rows will be generated later.
PlaceHolder1 Act as the container for the generated rows for tblPart1.
PlaceHolder2 Act as the container for the generated header and rows for tblPart2.
Table 1. Functional description of the objects

To freeze the header we can use the following CSS code:

tr.head
{
 position:relative; top: expression(document.all["divItems"].scrollTop-1); 
}

Building the horizontal scrollbar

To mimic IE scrollbar we can place a DIV tag inside a DIV tag. And resize both of them at run-time using JavaScript, this is done in ResizeTables() function.

 function ResizeTables()
 {         
  if (document.readyState)
  {  
       
   if (!document.all['divItems']) return;
   divItems.style.width = document.body.clientWidth-17;
   divItems.style.left = 0;        
       
   divPart2.style.width = document.body.clientWidth-155; 
   scroll1.style.width = divItems.style.width;
   scroll1.style.width = divItems.style.width;
   if(screen.width > 800)
    spacing1.style.width = tblPart2.offsetWidth+ tblPart1.offsetWidth;
   else
    spacing1.style.width = tblPart2.offsetWidth+ tblPart1.offsetWidth;          
           
   var AvailableHeight;
   AvailableHeight = 500;                
   divItems.style.height  = AvailableHeight;           
      
   divPart2.style.width = divItems.offsetWidth - tblPart1.offsetWidth-2;  
   
   if (parseInt(divPart2.style.width) + 
      tblPart1.offsetWidth < document.body.clientWidth )
   {  
    if (screen.width > 800)
    {              
     if(parseInt(divItems.style.height) < divPart2.offsetHeight)
     {                                                   
      divPart2.style.width = divItems.offsetWidth  -tblPart1.offsetWidth-19;
     }
    }
    else
    {
     if(parseInt(divItems.style.height) > divPart2.offsetHeight)
     {                         
      divPart2.style.width = divItems.offsetWidth -tblPart1.offsetWidth-2;
     }            
     else
     {
      divPart2.style.width = divItems.offsetWidth -tblPart1.offsetWidth-19;
     }           
    }

   }
  }
 }
 
 function OnScroll(sc)
 {  
  document.getElementById("divPart2").scrollLeft = sc.scrollLeft; 
 }         
 window.onload = ResizeTables;
 window.onresize = ResizeTables;
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralDownload link Not working
caresoft
20:05 23 Nov '09  
Can u please reupload the source file.
GeneralVisit this article on freeze header/column - http://www.codeproject.com/useritems/FreezeHeader.asp
Tittle Joseph
0:01 11 May '06  
Custom control to freeze header/column in datagrid in easiest way just changing single attribute value.

http://www.codeproject.com/useritems/FreezeHeader.asp[^]

---------
Tittle
GeneralRe: Visit this article on freeze header/column - http://www.codeproject.com/useritems/FreezeHeader.asp
squattyarun
8:26 16 Jun '08  
Great article, But It doesn't work for larger Grid control and also if grid contains combo box. Can you work on this also?? I am trying to sort it out from my side...

Arun Singh
Noida.

GeneralExcellant
nimesh123
11:03 1 Feb '06  
Excellant article, very helpful Thanks
GeneralNot working for me
hasnainlakhani
0:13 24 Jun '05  
Hi,

I am not getting any errors, getting the columns name, company, contact title.. nothing else. Let me know what exactly needs to be done to make it run

Hasnain
GeneralRe: Not working for me
linxuan
22:03 27 Feb '06  
I do not know how to add my data to the example,can you help me ?
Generaldoesn't work for me
newby
6:56 7 Oct '04  
1) dt.Rows.Count returns 0 rows. Dataset returns all the rows but doesn't fill datatable for some reason. Example displays blank page.
2) Do you have example code using C#?
3) Do you have same example code using Datagrid instead of Datatable?

Thanks

newby
Generalgood article
wu_jian830
17:25 4 Jul '04  
But You are inattention that your database password is PaNaSoNiC2003!
GeneralRe: good article
xiaofujian
23:57 6 Jun '07  
but it can be a good perfamance in firefox.Smile
Generalworks but jscript is slow
ben55124
10:33 1 Jun '04  
It works, but jscript is slow. Resizing the browser locks up my pc for a few seconds.

Would be nice if w3c.org would add native HTML support for freezing table rows and columns.


GeneralRe: works but jscript is slow
Anatoly Rapoport
21:07 1 Jun '04  
There is native support to freeze header in Netscape, but not in IE
GeneralRe: works but jscript is slow
irwansyah
0:05 7 Jun '04  
It's slow because I retrieve too many records than your browser can handle. Have you tried to modify the stored procedure so it's only retrieve a small size of records? I think it won't be slow again.
AnswerRe: works but jscript is slow
Midi
22:16 12 Sep '05  
I've read another thread in another website with the similiar problem, and It's related to IE DOM object performance which will run much slower when there are large number of records to render.

try to change the css from :

TR.head { PADDING-BOTTOM: 1px; FONT: menu; CURSOR: hand; COLOR: menutext; PADDING-TOP: 0px; POSITION: relative; ; TOP: expression(document.all["divItems"].scrollTop-1); HEIGHT: 25px; BACKGROUND-COLOR: #fce5b4 }

TO :

TR.head { PADDING-BOTTOM: 1px; FONT: menu; CURSOR: hand; COLOR: menutext; PADDING-TOP: 0px; POSITION: relative;
top: expression(parentNode.parentNode.parentNode.parentNode.scrollTop); HEIGHT: 25px; BACKGROUND-COLOR: #fce5b4 }


Smile MiDi


Last Updated 1 Jun 2004 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010