Introduction
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
public void AddSortDirectionImage(GridView gridviewID, GridViewRow itemRow)
{
if (gridviewID.AllowSorting == false)
return;
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)
{
itemRow.Cells[i].Controls.Add(space);
itemRow.Cells[i].Controls.Add(sortImage);
}
}
}
And FreezeGridviewHeader Function
protected void FreezeGridviewHeader(GridView _gv1, Table _tb1,Panel _pc1)
{
Page.EnableViewState = false;
_tb1.Rows.Add(_gv1.HeaderRow);
_tb1.Rows[0].ControlStyle.CopyFrom(_gv1.HeaderStyle);
_tb1.CellPadding = _gv1.CellPadding;
_tb1.CellSpacing = _gv1.CellSpacing;
_tb1.BorderWidth = _gv1.BorderWidth;
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 =
_gv1.Columns[Count].HeaderStyle.BorderWidth;
_tb1.Rows[0].Cells[Count].BorderStyle =
_gv1.Columns[Count].HeaderStyle.BorderStyle;
_pc1.Width = Unit.Pixel(Convert.ToInt32(
_tb1.Rows[0].Cells[Count].Width.Value) +
Convert.ToInt32(_pc1.Width.Value) + 14);
}
}
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.
History
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
http://www.grupoKino.com Antonio Suarez.