5,703,457 members and growing! (19,393 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Multi Header Grid view

By ZeroDev

I have been looking for a way to add headers to a grid view and to be able to re use this code this code snippet could help adding the headers to grid view
Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 30 Jul 2006
Updated: 30 Jul 2006
Views: 23,047
Bookmarked: 9 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
8 votes for this Article.
Popularity: 2.58 Rating: 2.86 out of 5
3 votes, 37.5%
1
0 votes, 0.0%
2
1 vote, 12.5%
3
1 vote, 12.5%
4
3 votes, 37.5%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article
I have been looking for a way to add headers to a grid view and to be able to re use this code this code snippet could help adding the headers to grid view

The Method that Dose the work:

Demo solution Downlod Download sourcecode

Description:

Gridview comes with a header for each column, you can't have an extra header to your Gridview, we can use the caption to some HTML tags and start building the extra headers, for me I did not like this solution, and came up with this code snippet

This method checks if the row of type header r then adds a row to the Gridview of type header by creating an object of type GridViewRow :

 row = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

to add the celss I Declaird an IDictionaryEnumerator that will count the cells passed by a sorted list , then within the sorted list I creat an arry of strings that ill be used to set the cells properties , an d last adding the Cells to the row

IDictionaryEnumerator enumCels = GetCels.GetEnumerator(); 
/// <summary>

    /// Gets my multi header.

    /// </summary>

    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>

    /// <param name="GetCels">The get cels. is sorted list that contain the cells the key is the position of the cell and the valu is a comma delemeted 

    ///  data to hold the cell information , index 0 is for the Content of the cell m index 1 is the coms span , index 2 is the row span 

    ///  pleas dont leav the colmn span and rowspan empty</param>

    public void GetMyMultiHeader(GridViewRowEventArgs e, SortedList GetCels)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {

GridViewRow row;

IDictionaryEnumerator enumCels = GetCels.GetEnumerator();

"MARGIN: 0in 0in 0pt; DIRECTION: ltr; unicode-bidi: embed; TEXT-ALIGN: left; mso-layout-grid-align: none">          

            row = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

while (enumCels.MoveNext())

{

string[] cont = enumCels.Value.ToString().Split(Convert.ToChar(","));

TableCell Cell;

Cell = new TableCell();

Cell.RowSpan = Convert.ToInt16(cont[2].ToString());

Cell.ColumnSpan = Convert.ToInt16(cont[1].ToString());

Cell.Controls.Add(new LiteralControl(cont[0].ToString()));

Cell.HorizontalAlign = HorizontalAlign.Center;

Cell.ForeColor = System.Drawing.Color.White;

row.Cells.Add(Cell);

}

e.Row.Parent.Controls.AddAt(0, row);

        }

   }

How to use the methode we have to use a sorted list as so

the key of the sorted list is the cell position , and the contetn of the

sell is the valu with a comma delemeted the first index is the text , the next index is the colmn span , and the 3ed index is the row span

like this example

 

/// <summary>

/// Handles the RowDataBound event of the GridViewData control.

/// </summary>

/// <param name="sender">The source of the event.</param>

/// <param name="e">The <see cref="T:System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>

protected void GridViewData_RowDataBound(object sender, GridViewRowEventArgs e)

{

SortedList creatCels = new SortedList();

creatCels.Add("1", ",5,2");

creatCels.Add("2", "Comments,4,1");

creatCels.Add("3", ",1,2");

SortedList creatCels2 = new SortedList();

creatCels2.Add("1", "Total Amount of,2,1");

creatCels2.Add("2", "Total Amount of,2,1");

GetMyMultiHeader(e, creatCels2);

GetMyMultiHeader(e, creatCels);

}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ZeroDev


Life is really simple, but we insist on making it complicated
Occupation: Chief Technology Officer
Location: Jordan Jordan

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
GeneralThanksmemberyokitocain20:51 29 Aug '08  
GeneralSample LayoutmemberWyatt Wong1:28 5 May '08  
GeneralUse RowCreated instead of RowDataBound eventmemberMichael Freidgeim2:01 6 Mar '08  
QuestionA posible Error...memberShunete.2:34 12 Sep '07  
AnswerRe: A posible Error...memberZeroDev2:55 12 Sep '07  
GeneralRe: A posible Error...memberShunete.3:56 12 Sep '07  
GeneralRe: A posible Error...memberZeroDev4:07 12 Sep '07  
GeneralRe: A posible Error...memberShunete.4:37 12 Sep '07  
GeneralRe: A posible Error...memberZeroDev5:01 12 Sep '07  
GeneralRe: A posible Error...memberShunete.5:18 12 Sep '07  
GeneralRe: A posible Error...memberZeroDev5:31 12 Sep '07  
GeneralRe: A posible Error...memberShunete.5:47 12 Sep '07  
GeneralSolved my problemmemberpauloya1:37 12 Sep '07  
GeneralGood Workmembermyabhi301:17 28 Aug '07  
GeneralRe: Good WorkmemberZeroDev4:03 28 Aug '07  
GeneralTHANK YOUmemberJeromeDeCuyper16:11 20 Feb '07  
GeneralRe: THANK YOUmemberZeroDev22:39 13 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Jul 2006
Editor:
Copyright 2006 by ZeroDev
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project