Click here to Skip to main content
14,383,763 members

Scrollable Gridview with Freeze header and sort image.

Rate this:
4.35 (19 votes)
Please Sign up or sign in to vote.
4.35 (19 votes)
14 May 2008CPOL
this is an easy way to get Freeze GridView header and add image sort. And it works on IE,Firefox and Others. work inside and outside of updatepanel.



I was searching for a way to get a scrollable Gridview with a freeze header and I found different ways to do it, but most of them use CSS or JavaScript. What I want to find was a way to do it in the code file. (.CS or .VB)

Here I would try to explain how it could be done with code. (.CS)

Using the Code

You just need to add a Panel control then add a Table and Gridview control inside of Panel control.

in the .CS file you need to add this code:

AddSortDirectionImage Function

/// summary
///   Email:
///Web Page:
/// [Español]Funcion que agrega una imagen a la columna ordenada de un gridView
/// [English]Function that adds a image to the sorted column.
/// summary
/// param name="gridviewID"
/// [Español]Gridview al cual se le agregara la imagen
/// [English]GridView to add sort image
/// param
/// param name="itemRow"
/// [Español]Renglon del gridview al cual se le agrega la imagen
/// [English]Row of gridview to add sort image
/// param

public void AddSortDirectionImage(GridView gridviewID, GridViewRow itemRow)
    if (gridviewID.AllowSorting == false)

    Image sortImage = new Image();
    Label space = new Label();

    sortImage.ImageUrl = (
        gridviewID.SortDirection ==
        SortDirection.Ascending ? @"~/sort_asc.gif" : @"~/sort_desc.gif");
    sortImage.Visible = true;
    space.Text = " ";

    for (int i = 0; i < gridviewID.Columns.Count; i++)
        string colExpr = gridviewID.Columns[i].SortExpression;
        if (colExpr != "" && colExpr == gridviewID.SortExpression)

And FreezeGridviewHeader Function

/// summary
///  Author: Antonio Suarez
///   Email:
///Web Page:
/// [Español]Funcion que copia el header de un gridview a un control tipo Table
/// [English]Function that copy a gridview header to a table control.
/// summary
/// param name="_gv1"
/// [Español]Gridview del cual se copiara el encabezado.
/// [English]GridView from where the header row will be copied
/// param
/// param name="_tb1"
/// [Español]Renglon del gridview al cual se le agrega la imagen
/// [English]Row of gridview to add sort image
/// param name="_pc1"
/// [Español]Panel que contendra tanto al gridview como al control table
/// [English]Panel container of the gridview and table control.
/// param
protected void FreezeGridviewHeader(GridView _gv1, Table _tb1,Panel _pc1)
    Page.EnableViewState = false;

    //[Español]Copiando las propiedades del renglon de encabezado
    //[English]Copying a header row data and properties
    _tb1.CellPadding = _gv1.CellPadding;
    _tb1.CellSpacing = _gv1.CellSpacing;
    _tb1.BorderWidth = _gv1.BorderWidth;

    //if (!_gv1.Width.IsEmpty)
    //_gv1.Width = Unit.Pixel(Convert.ToInt32(_gv1.Width.Value) + Convert.ToInt32(
    //    _tb1.Width.Value) + 13);

    //[Español]Copiando las propiedades de cada celda del nuevo encabezado.
    //[English]Copying  each cells properties to the new header cells properties
    int Count = 0;
    _pc1.Width = Unit.Pixel(100);
    for (Count = 0; Count < _gv1.HeaderRow.Cells.Count - 1; Count++)
        _tb1.Rows[0].Cells[Count].Width = _gv1.Columns[Count].ItemStyle.Width;
        _tb1.Rows[0].Cells[Count].BorderWidth =
        _tb1.Rows[0].Cells[Count].BorderStyle =
        _pc1.Width = Unit.Pixel(Convert.ToInt32(
            _tb1.Rows[0].Cells[Count].Width.Value) +
            Convert.ToInt32(_pc1.Width.Value) + 14);
    //Panel1.Width = Unit.Pixel(Convert.ToInt32(
    //    _tb1.Rows[0].Cells[Count-1].Width.Value) + 12);

Points of Interest

The point here is that it should work in the most of the graphical browser and we will not have to add fix for each browser compatibility.


We need to add edit functionality. I hope all this could be useful for you, if this work fine.

Other thing, if you have a page please add my web page link

Antonio Suarez.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Antonio Suarez
Web Developer Health Company
Mexico Mexico
Working as:
Web Developer.

Free Time:
Web Developer.

Espero no esperar y mientras mas espero, espero que esperar.

Comments and Discussions

Questioncan not dim as new image Pin
Member 834559923-May-14 11:53
memberMember 834559923-May-14 11:53 
GeneralMy vote of 1 Pin
Sanjay K. Gupta29-Aug-12 1:20
professionalSanjay K. Gupta29-Aug-12 1:20 
GeneralMy vote of 3 Pin
Vinayak B15-Feb-12 21:08
memberVinayak B15-Feb-12 21:08 
GeneralMy vote of 1 Pin
deveshbhattacharya17-Oct-10 4:04
memberdeveshbhattacharya17-Oct-10 4:04 
GeneralWorks with a static grid... Pin
asdqwewqe13-Dec-08 10:11
memberasdqwewqe13-Dec-08 10:11 
GeneralVery bad solution Pin
nsimeonov5-Dec-08 15:53
membernsimeonov5-Dec-08 15:53 
GeneralRe: Very bad solution Pin
Antonio Suarez5-Dec-08 16:23
memberAntonio Suarez5-Dec-08 16:23 
GeneralMy vote of 1 Pin
nsimeonov5-Dec-08 15:51
membernsimeonov5-Dec-08 15:51 
QuestionIts not working?????? Pin
Nitish.11272-Nov-08 18:49
memberNitish.11272-Nov-08 18:49 
AnswerHere I am posted the small and workable C# code which work with the panel also Pin
Nitish.11273-Nov-08 0:25
memberNitish.11273-Nov-08 0:25 
Generalit won't work Pin
imran prasla24-Sep-08 9:09
memberimran prasla24-Sep-08 9:09 
GeneralRe: it won't work Pin
Antonio Suarez24-Sep-08 9:21
memberAntonio Suarez24-Sep-08 9:21 
GeneralRe: it won't work Pin
imran prasla25-Sep-08 4:36
memberimran prasla25-Sep-08 4:36 
GeneralRe: it won't work Pin
Antonio Suarez25-Sep-08 6:17
memberAntonio Suarez25-Sep-08 6:17 
GeneralRe: it won't work Pin
kirtiarora1-Mar-12 18:01
memberkirtiarora1-Mar-12 18:01 
Questionaspx page coding for freeze???????? Pin
qtrmile691-Aug-08 9:21
memberqtrmile691-Aug-08 9:21 
Answeraddtl.....: aspx page coding for freeze???????? Pin
qtrmile691-Aug-08 9:23
memberqtrmile691-Aug-08 9:23 
GeneralEdit functionality Pin
scott.m.owen24-Jul-08 8:43
memberscott.m.owen24-Jul-08 8:43 
GeneralRe: Edit functionality Pin
Antonio Suarez24-Jul-08 21:29
memberAntonio Suarez24-Jul-08 21:29 
GeneralRe: Edit functionality Pin
tommymro21-Nov-08 9:02
membertommymro21-Nov-08 9:02 
GeneralTwo identical header rows Pin
scottlis27-Jun-08 3:19
memberscottlis27-Jun-08 3:19 
GeneralRe: Two identical header rows Pin
Antonio Suarez24-Jul-08 21:45
memberAntonio Suarez24-Jul-08 21:45 
General[Message Removed] Pin
Mojtaba Vali23-May-08 23:10
memberMojtaba Vali23-May-08 23:10 
GeneralImprovements Pin
Stefan Kraft23-May-08 12:54
memberStefan Kraft23-May-08 12:54 
GeneralRe: Improvements Pin
Antonio Suarez23-May-08 13:20
memberAntonio Suarez23-May-08 13:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 14 May 2008


85 bookmarked