Click here to Skip to main content
15,881,852 members
Articles / Web Development / ASP.NET
Article

Multi Header Grid view

Rate me:
Please Sign up or sign in to vote.
3.13/5 (10 votes)
30 Jul 20061 min read 98.6K   1.5K   23   26
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
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)
        {<P>GridViewRow row;
<P>            IDictionaryEnumerator enumCels = GetCels.GetEnumerator();
<PRE class=MsoNormal style="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

 <P>/// <summary></P>
<P>    /// Handles the RowDataBound event of the GridViewData control.</P>
<P>    /// </summary></P>
<P>    /// <param name="sender">The source of the event.</param></P>
<P>    /// <param name="e">The <see cref="T:System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param></P>

<P>protected void GridViewData_RowDataBound(object sender, GridViewRowEventArgs e)</P>
<P>    {</P>



<P>        SortedList creatCels = new SortedList();</P>
<P>        creatCels.Add("1", ",5,2");</P>
<P>        creatCels.Add("2", "Comments,4,1");</P>
<P>        creatCels.Add("3", ",1,2");</P>

<P>        SortedList creatCels2 = new SortedList();</P>
<P>        creatCels2.Add("1", "Total Amount of,2,1");</P>
<P>        creatCels2.Add("2", "Total Amount of,2,1");</P>


<P>        GetMyMultiHeader(e, creatCels2);</P>
<P>        GetMyMultiHeader(e, creatCels);</P>
    }

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


Written By
Chief Technology Officer
Jordan Jordan
Life is really simple, but we insist on making it complicated

Comments and Discussions

 
GeneralIt's a great idea Pin
Lechu8123-Aug-12 11:31
Lechu8123-Aug-12 11:31 
GeneralRe: It's a great idea Pin
ZeroDev23-Aug-12 12:37
ZeroDev23-Aug-12 12:37 
BugRe: It's a great idea Pin
Lechu8127-Aug-12 9:04
Lechu8127-Aug-12 9:04 
GeneralRe: It's a great idea Pin
ZeroDev27-Aug-12 10:40
ZeroDev27-Aug-12 10:40 
GeneralRe: It's a great idea Pin
Lechu8127-Aug-12 10:52
Lechu8127-Aug-12 10:52 
GeneralRe: It's a great idea Pin
Lechu8127-Aug-12 11:00
Lechu8127-Aug-12 11:00 
GeneralRe: It's a great idea Pin
bsshowriraju000122-Nov-12 18:03
bsshowriraju000122-Nov-12 18:03 
GeneralThank You Pin
Member 44203723-Sep-09 4:52
Member 44203723-Sep-09 4:52 
GeneralProblem with Multi Header Pin
kamal kishor30-Apr-09 3:34
kamal kishor30-Apr-09 3:34 
GeneralThanks Pin
yokitocain29-Aug-08 19:51
yokitocain29-Aug-08 19:51 
GeneralSample Layout Pin
Wyatt Wong5-May-08 0:28
Wyatt Wong5-May-08 0:28 
GeneralUse RowCreated instead of RowDataBound event Pin
Michael Freidgeim6-Mar-08 1:01
Michael Freidgeim6-Mar-08 1:01 
QuestionA posible Error... Pin
Shunete.12-Sep-07 1:34
Shunete.12-Sep-07 1:34 
AnswerRe: A posible Error... Pin
ZeroDev12-Sep-07 1:55
ZeroDev12-Sep-07 1:55 
GeneralRe: A posible Error... Pin
Shunete.12-Sep-07 2:56
Shunete.12-Sep-07 2:56 
GeneralRe: A posible Error... Pin
ZeroDev12-Sep-07 3:07
ZeroDev12-Sep-07 3:07 
GeneralRe: A posible Error... Pin
Shunete.12-Sep-07 3:37
Shunete.12-Sep-07 3:37 
GeneralRe: A posible Error... Pin
ZeroDev12-Sep-07 4:01
ZeroDev12-Sep-07 4:01 
GeneralRe: A posible Error... Pin
Shunete.12-Sep-07 4:18
Shunete.12-Sep-07 4:18 
GeneralRe: A posible Error... Pin
ZeroDev12-Sep-07 4:31
ZeroDev12-Sep-07 4:31 
GeneralRe: A posible Error... Pin
Shunete.12-Sep-07 4:47
Shunete.12-Sep-07 4:47 
GeneralSolved my problem Pin
pauloya12-Sep-07 0:37
pauloya12-Sep-07 0:37 
GeneralGood Work Pin
myabhi3028-Aug-07 0:17
myabhi3028-Aug-07 0:17 
GeneralRe: Good Work Pin
ZeroDev28-Aug-07 3:03
ZeroDev28-Aug-07 3:03 
GeneralTHANK YOU Pin
JeromeDeCuyper20-Feb-07 15:11
JeromeDeCuyper20-Feb-07 15:11 

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.