GridView composite multi-layer header (unlimited )!!!

Source: Internet
Author: User

Header definition description:
Table header definition method: The headers of adjacent parent columns are separated by '#', the upper-level row and the lower-level row are separated by spaces (''), and the adjacent child column headers are separated by commas (', ').
Table header definition example:
A. Two Layers

Tobacco grade # level # Baoshan, Baoshan, subtotal # Yao 'an, Chuxiong, subtotal # binchuan, Dali, and subtotal # shuijian, Luxi, Maitreya, Shi Ping, subtotal # Kunming Fumin, Luquan, subtotal # huaping, ninglang, subtotal # Cangyuan, Fengqing, Gengma, linxiang, shuangjiang, yongde, Yunxian, zhenkang, subtotal # Jing Dong, Simao, zhenshu, and subtotal # Fu Yuan, Qujing City, lu Liang, Luo Ping, Shi Zong, Xuan Wei, subtotal # Wenshan, subtotal # Yuxi, subtotal # Total
B. Layer 3

 

Level # level # Number of stored items in the previous period, weight, proportion # number of transferred items in the current phase, weight, proportion # number of items delivered to the workshop in the current period, weight, proportion # number of items exported to products in the current period, weight, proportion # Average Value

Call Description: Add the following code to the RowCreated event of the GridView during use.

If (e. Row. RowType = DataControlRowType. Header)
...{
DynamicTHeaderHepler dHelper = new DynamicTHeaderHepler ();
String header = "level # Number of stored items in the previous period, weight, proportion # number of transferred items in the current phase, weight, proportion # Number of materials in the workshop issued in the current period, weight ,"
+ "Proportion # number, weight, proportion of products exported in the current period # average value ";

DHelper. SplitTableHeader (e. Row, header );
}
Header generation class:

//
//************************************** *********************************
// Created: 2007-10-29 Author: ruijc
// File: DynamicTHeaderHepler. cs
// Description: The help class for dynamically generating composite Headers
// The headers of adjacent parent columns are separated by '#'. The headers of parent and child columns are separated by spaces (''). The headers of adjacent child columns are separated by commas (',').
//************************************** *********************************
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Collections. Generic;
Using System. Collections;
Public class DynamicTHeaderHepler
...{
Public DynamicTHeaderHepler ()
...{
//
// TODO: Add constructor logic here
//
}

/** // <Summary>
/// Rewrite the header
/// </Summary>
/// <Param name = "targetHeader"> Target header </param>
/// <Param name = "newHeaderNames"> new header </param>
/// <Remarks>
/// Level # Number of stored items in the previous period, weight, proportion # number of transferred items in the current phase, weight, proportion # Number of materials delivered to the workshop in the current period, weight,
/// Proportion # number, weight, and proportion of products exported in the current period # Average Value
/// </Remarks>
Public void SplitTableHeader (GridViewRow targetHeader, string newHeaderNames )...{
TableCellCollection tcl = targetHeader. Cells; // retrieves an instance of the Header element.
Tcl. Clear (); // Clear the element
Int row = GetRowCount (newHeaderNames );
Int col = GetColCount (newHeaderNames );
String [,] nameList = ConvertList (newHeaderNames, row, col );
Int RowSpan = 0;
Int ColSpan = 0;
For (int k = 0; k <row; k ++)
...{
String LastFName = "";
For (int I = 0; I <col; I ++)
...{
If (LastFName = nameList [I, k] & k! = Row-1)
...{
LastFName = nameList [I, k];
Continue;
}
Else
...{
LastFName = nameList [I, k];
}
Int bFlag = IsVisible (nameList, k, I, LastFName );
Switch (bFlag)
...{
Case 0:
Break;
Case 1:
RowSpan = GetSpanRowCount (nameList, row, k, I );
ColSpan = GetSpanColCount (nameList, row, col, k, I );
Tcl. Add (new TableHeaderCell (); // Add the header Control
Tcl [tcl. Count-1]. RowSpan = RowSpan;
Tcl [tcl. Count-1]. ColumnSpan = ColSpan;
Tcl [tcl. Count-1]. HorizontalAlign = HorizontalAlign. Center;
Tcl [tcl. Count-1]. Text = LastFName;
Break;
Case-1:
String [] EndColName = LastFName. Split (new char []... {','});
Foreach (string eName in EndColName )...{
Tcl. Add (new TableHeaderCell (); // Add the header Control
Tcl [tcl. Count-1]. HorizontalAlign = HorizontalAlign. Center;
Tcl [tcl. Count-1]. Text = eName;
}
Break;
}
}
If (k! = Row-1)
... {// Not the starting line. Add a new line label
Tcl [tcl. Count-1]. Text = tcl [tcl. Count-1]. Text + "</th> </tr> <tr> ";
}
}
}

/** // <Summary>
/// If the previous row has output the same column header as the current content, it is not displayed
/// </Summary>
/// <Param name = "ColumnList"> header set </param>
/// <Param name = "rowIndex"> row index </param>
/// <Param name = "colIndex"> column index </param>
/// <Returns> 1: Display,-1: contain ',' separator, 0: not displayed </returns>
Private int IsVisible (string [,] ColumnList, int rowIndex, int colIndex, string CurrName)
...{
If (rowIndex! = 0 )...{
If (ColumnList [colIndex, rowIndex-1] = CurrName )...{
Return 0;
} Else ...{
If (ColumnList [colIndex, rowIndex]. Contains (","))
...{
Return-1;
} Else ...{
Return 1;
}
}
}
Return 1;
}

/** // <Summary>
/// Obtain the number of lines that are different from the content of the lower-level columns corresponding to the current index row and column
/// </Summary>
/// <Param name = "ColumnList"> header set </param>
/// <Param name = "row"> Number of rows </param>
/// <Param name = "rowIndex"> row index </param>
/// <Param name = "colIndex"> column index </param>
/// <Returns> Number of rows </returns>
Private int GetSpanRowCount (string [,] ColumnList, int row, int rowIndex, int colIndex)
...{
String LastName = "";
Int RowSpan = 1;
For (int k = rowIndex; k <row; k ++)
...{
If (ColumnList [colIndex, k] = LastName )...{
RowSpan ++;
} Else ...{
LastName = ColumnList [colIndex, k];
}
}
Return RowSpan;
}

/** // <Summary>
/// Obtain the number of columns that are different from the content of the lower-level corresponding to the current index row and column
/// </Summary>
/// <Param name = "ColumnList"> header set </param>
/// <Param name = "row"> Number of rows </param>
/// <Param name = "col"> Number of columns </param>
/// <Param name = "rowIndex"> row index </param>
/// <Param name = "colIndex"> column index </param>
/// <Returns> Number of columns </returns>
Private int GetSpanColCount (string [,] ColumnList, int row, int col, int rowIndex, int colIndex )...{
String LastName = ColumnList [colIndex, rowIndex];
Int ColSpan = ColumnList [colIndex, row-1]. Split (new char []... {','}). Length;
ColSpan = 1? 0: ColSpan;
For (int I = colIndex + 1; I <col; I ++ )...{
If (ColumnList [I, rowIndex] = LastName)
...{
ColSpan + = ColumnList [I, row-1]. Split (new char []... {','}). Length;
}
Else
...{
LastName = ColumnList [I, rowIndex];
Break;
}
}
Return ColSpan;
}

/** // <Summary>
/// Save the defined header to an array
/// </Summary>
/// <Param name = "newHeaders"> new header </param>
/// <Param name = "row"> Number of rows </param>
/// <Param name = "col"> Number of columns </param>
/// <Returns> header array </returns>
Private string [,] ConvertList (string newHeaders, int row, int col)
...{
String [] ColumnNames = newHeaders. Split (new char []... {'#'});
String [,] news = new string [col, row];
String Name = "";
For (int I = 0; I <col; I ++)
...{
String [] CurrColNames = ColumnNames [I]. ToString (). Split (new char []... {''});
For (int k = 0; k <row; k ++)
...{
If (CurrColNames. Length-1> = k)
...{
If (CurrColNames [k]. Contains (","))
...{
If (CurrColNames. Length! = Row)
...{
If (Name = "")
...{
News [I, k] = news [I, k-1];
Name = CurrColNames [k]. ToString ();
}
Else
...{
News [I, k + 1] = Name;
Name = "";
}
} Else ...{
News [I, k] = CurrColNames [k]. ToString ();
}
} Else ...{
News [I, k] = CurrColNames [k]. ToString ();
}
} Else ...{
If (Name = "")
...{
News [I, k] = news [I, k-1];
} Else ...{

News [I, k] = Name;
Name = "";
}
}
}
}
Return news;
}

/** // <Summary>
/// Obtain the number of rows in the composite Header
/// </Summary>
/// <Param name = "newHeaders"> new header </param>
/// <Returns> Number of rows </returns>
Private int GetRowCount (string newHeaders)
...{
String [] ColumnNames = newHeaders. Split (new char []... {'#'});
Int Count = 0;
Foreach (string name in ColumnNames )...{
Int TempCount = name. Split (new char []... {''}). Length;
If (TempCount> Count)
Count = TempCount;
}
Return Count;
}

/** // <Summary>
/// Obtain the number of columns in the composite Header
/// </Summary>
/// <Param name = "newHeaders"> new header </param>
/// <Returns> Number of columns </returns>
Private int GetColCount (string newHeaders )...{
Return newHeaders. Split (new char []... {'#'}). Length;
}

}

Http://blog.csdn.net/aoeagle/archive/2008/02/20/2108567.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.