C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Freezing Headers of the DataGrid in ASP.NET


Posted Date: 12 Jan 2005    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Bezawada Kiran KumarMember Level: Silver    
Rating: 1 out of 5Points: 10



Place a datagrid control on the form and in the page load event bind the datagrid to the dataset after getting the data.(Place the datagrid in a div tag and name the div tag as div-datagrid).
Use bound columns which will be easier to bind the data.

 
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
SqlConnection cn=new SqlConnection
("server=server-name;uid=Test;password=Test;database=database-name");
cn.Open();
SqlDataAdapter dad=new SqlDataAdapter("select * from emp",cn);
DataSet ds=new DataSet();
dad.Fill(ds,"EmpTable");
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}

Use the following style in the head part of the html.

<style>
DIV#div-datagrid { OVERFLOW: auto; WIDTH: 420px; HEIGHT: 200px }
.lockrow
{
BORDER-RIGHT: silver 1px solid;
FONT-WEIGHT: bold;
FONT-SIZE: 14px; ;
TOP: expression(document.getElementById("div-datagrid").scrollTop-2);
COLOR: white;
POSITION: relative;
BACKGROUND-COLOR: green;
TEXT-ALIGN: center;height:30
}
</style>

In the code behind page of the asp.net datagrid event write the following code.

private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch(e.Item.ItemType)
{
case ListItemType.Header:
for(int i=0;i<=e.Item.Cells.Count-1;i++)
{
e.Item.Cells[i].CssClass="lockrow";
}
break;
}
}



Responses

Author: krishna mohan    13 Jan 2005Member Level: Silver   Points : 0
Use the following style in the head part of the html.
but i didn't see that code...
plz include that code , i am trying yours logic....

thank you
kittu


Author: critic    13 Jan 2005Member Level: Bronze   Points : 0
Please check the article now.. we have updated it so that the style section is visible.


Author: Anand Fadte    04 Feb 2005Member Level: Bronze   Points : 0
I tried using the code, but still doesn't work, can u help me out


Author: Bezawada Kiran Kumar    04 Feb 2005Member Level: Silver   Points : 0
Send me the code. so that i can clarify/.


Author: Bezawada Kiran Kumar    04 Feb 2005Member Level: Silver   Points : 0
Send me the code. so that i can clarify/.And pls explain ur problem in detail.


Author: Hilmar Hilmarsson    08 Feb 2005Member Level: Bronze   Points : 0
Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you ..... I've been looking everywhere for this. Thank you.


Author: LeninGrad    23 Feb 2005Member Level: Silver   Points : 0
This is what i am searching for all these days. Your article is excellent. I do not have any words to explain. I had been searching for this article in various web sites but no clear explanation. Thanks to dotnetspider.com to provide such a nice article.

Great applause to this guy


Author: Sue oates    27 Feb 2005Member Level: Bronze   Points : 0
Hi,
I have been searching for code to implement a fixed header datagrid.

I have attempted to implement the code provided for Freezing Headers of the DataGrid in ASP.NET in VB code.

I have made the following changes to your method Datagrid1_ItemDataBound:


Private Sub dgBasins_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgBasins.ItemDataBound
Select Case e.Item.ItemType
Case ListItemType.Header
Dim i As Integer
For i = 0 To e.Item.Cells.Count - 1
e.Item.Cells(i).CssClass = "lockrow"
Next

End Select
End Sub

When I run this, the page header style certainly changes with green background and white font. However, the header still scrolls off the top of the div-datagrid.

I was under the understanding that the header did not scroll. Is this correct, and if so can you highlight the error in my code.

Thanks in anticipation of your help.


Author: surya phani    03 Mar 2005Member Level: Bronze   Points : 0
Hey buddy, it works, Thanks a lot. I used to set a HTML table above the grid earlier instead of using the header. Thanks to u.


Author: Jagadish Dandin    08 Sep 2005Member Level: Bronze   Points : 0
Everything is fine. But the columns in the headers cannot be differentiated. The lines that differentiate different columns disappear when this style is applied to header.

Any solution for this would be appreciated.

Thanks in advance.


Author: Jagadish Dandin    08 Sep 2005Member Level: Bronze   Points : 0
Hi
I figured out the problem. the solution suggested above works perfectly.

if we try to apply the style on the client side in the
the column lines will not be visible. If we apply the style as suggested

switch(e.Item.ItemType)
{
case ListItemType.Header:
for(i=0;i<=e.Item.Cells.Count-1;i++)
{
e.Item.Cells[i].CssClass="lockrow";
}
break;
}

it works perfectly.

regards
Jagadish



Author: Jagadish Dandin    08 Sep 2005Member Level: Bronze   Points : 0
Hi
I figured out the problem.
if we try to apply the style on the client side in the header style



the column lines will not be visible. If we apply the style as suggested in the article

switch(e.Item.ItemType)
{
case ListItemType.Header:
for(i=0;i<=e.Item.Cells.Count-1;i++)
{
e.Item.Cells[i].CssClass="lockrow";
}
break;
}

it works perfectly.





Author: HDave    13 Dec 2005Member Level: Bronze   Points : 0
Nice!! Excellent solution!!


Author: HDave    13 Dec 2005Member Level: Bronze   Points : 0
//============= Grid.CSS ================
BODY
{
}
.Grid
{
BACKGROUND-COLOR: WhiteSmoke;
BORDER: #1F4597 1px solid;
FONT-SIZE: 12px;
FONT-FAMILY: Tahoma;
COLOR: Black;
}

.Grid td
{
BORDER: #1F4597 1px solid;
PADDING: 3px;
}

.GridColHeader
{
BACKGROUND-COLOR: #1F4597;
FONT-SIZE: 12px;
FONT-FAMILY: Tahoma;
FONT-WEIGHT: bold;
COLOR: White;
TEXT-TRANSFORM: capitalize;
}

.GridColFooter
{
BACKGROUND-COLOR: #CCCCCC;
}

.GridItem
{
BACKGROUND-COLOR: Transparent;
FONT-SIZE: 12px;
FONT-FAMILY: Tahoma;
COLOR: Black;
}

.GridAltItem
{
BACKGROUND-COLOR: Ivory;
FONT-SIZE: 12px;
FONT-FAMILY: Tahoma;
COLOR: Black;
}

DIV#div-datagrid { OVERFLOW: auto; WIDTH: 100%; HEIGHT: 200px }
.LockRow
{
TOP: expression(document.getElementById("div-datagrid").scrollTop-2);
POSITION: relative;
}

//============= Grid.CSS ================

//============= ASPX ==================




//DataGrid







//============= ASPX ==================


//



Author: HDave    13 Dec 2005Member Level: Bronze   Points : 0
//============= Grid.CSS ================
BODY
{
}
.Grid
{
BACKGROUND-COLOR: WhiteSmoke;
BORDER: #1F4597 1px solid;
FONT-SIZE: 12px;
FONT-FAMILY: Tahoma;
COLOR: Black;
}

.Grid td
{
BORDER: #1F4597 1px solid;
PADDING: 3px;
}

.GridColHeader
{
BACKGROUND-COLOR: #1F4597;
FONT-SIZE: 12px;
FONT-FAMILY: Tahoma;
FONT-WEIGHT: bold;
COLOR: White;
TEXT-TRANSFORM: capitalize;
}

.GridColFooter
{
BACKGROUND-COLOR: #CCCCCC;
}

.GridItem
{
BACKGROUND-COLOR: Transparent;
FONT-SIZE: 12px;
FONT-FAMILY: Tahoma;
COLOR: Black;
}

.GridAltItem
{
BACKGROUND-COLOR: Ivory;
FONT-SIZE: 12px;
FONT-FAMILY: Tahoma;
COLOR: Black;
}

DIV#div-datagrid { OVERFLOW: auto; WIDTH: 100%; HEIGHT: 200px }
.LockRow
{
TOP: expression(document.getElementById("div-datagrid").scrollTop-2);
POSITION: relative;
}

//============= Grid.CSS ================

//============= ASPX ==================




//DataGrid







//============= ASPX ==================


//



Author: HDave    13 Dec 2005Member Level: Bronze   Points : 0
//==============ASPX=================


//==============ASPX=================


Author: HDave    13 Dec 2005Member Level: Bronze   Points : 0
//

//
//
//
//
//
//

//



Author: HDave    13 Dec 2005Member Level: Bronze   Points : 0
//div id=div-datagrid { OVERFLOW: auto; WIDTH: "100%"; HEIGHT: 400px >
//asp:datagrid id="dgBatchLog" runat="server" Width="100%" CssClass="Grid">
//
//
//
//
///asp:datagrid>
///div>


Author: HDave    13 Dec 2005Member Level: Bronze   Points : 0
Note: replace "// with "<"

//div id=div-datagrid { OVERFLOW: auto; WIDTH: "100%"; HEIGHT: 400px >
//asp:datagrid id="dgBatchLog" runat="server" Width="100%" CssClass="Grid">
//FooterStyle CssClass="GridColFooter">
//AlternatingItemStyle CssClass="GridAltItem">
//ItemStyle CssClass="GridItem">
//HeaderStyle CssClass="GridColHeader">
///asp:datagrid>
///div>


Author: Sirisha Akella    02 Feb 2006Member Level: Bronze   Points : 0
But I still do have a problem. Actually, I have a datagrid with a template column with dropdown and other bound columns. Rest of the columns are working fine, but with this particular template column, the dropdown is scolling over the corresponding header text. Please help


Author: Bernie Miller    14 Mar 2007Member Level: Bronze   Points : 0
When applied to a multi-line header, headings that span multiple rows are chopped off so you only see the top half of the text. Any ideas on how to correct this? Thanks.


Author: Bernie Miller    15 Mar 2007Member Level: Bronze   Points : 0
Hello, when combined with the multi-line header solution at http://www.codeproject.com/aspnet/MergeDatagridHeader.asp the second row of a rowspan=2 heading is always blank. If I move the text to the first row it appears fine. Centered in the middle and half the text is gone. Moved to the bottom row and it completely disappears. It's not a huge issue but I would prefer the heading to be centered or on the bottom row. Any ideas why this is happening? Thanks.


Author: Bernie Miller    21 Mar 2007Member Level: Bronze   Points : 0
Hello, all CSS solutions seem to have the same issue. You cannot center the table. If you do the header will no longer align with the data when the page is re-sized. I have yet to come across a solution to this limitation.


Author: Rajkumar    01 Aug 2007Member Level: Silver   Points : 0
Hi,

I tried with your piece of code.. It worked well.. Its very simple and very useful..Thanks..

By,
P.T.RAJKUMAR


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Tips for Programming ASP.NET
Previous Resource: ASP.Net Performance
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use