![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 方法编号:05
/// 方法名称:SetMultiLayerHeader
/// 内容摘要:创建DataGrid多层表头
/// 算法思路:建立一个ArrayList(1)来放置表征表头的ArrayList(2),通过这种结构来自动生成多层表头
/// 数组2的格式要求为:"内容,[rowspan/colspan],[colspan/colspan的值],[样式哈希表]"(带[]的可选)
/// 其中[样式哈希表]的为:key/value的对应关系,如Hashtable_style.Add("Font-size","12pt");
/// </summary>
public void SetMultiLayerHeader(DataGrid m_grid,
System.Web.UI.WebControls.DataGridItemEventArgs e,
ArrayList m_headers)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
e.Item.Visible = false;
![](/Images/OutliningIndicators/InBlock.gif)
//处理特殊情况
if (m_grid.Controls.Count <= 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
![](/Images/OutliningIndicators/InBlock.gif)
Table m_tb = (Table)m_grid.Controls[0];
int m_iTmp = 0;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
处理多行表头的实体#region 处理多行表头的实体
foreach (object obj in m_headers)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DataGridItem m_dg = new DataGridItem(0,0,ListItemType.Header);
ArrayList m_arrHeader = (ArrayList)obj;
![](/Images/OutliningIndicators/InBlock.gif)
TableCell[] m_cells = new TableCell[m_arrHeader.Count];
for (int i=0; i< m_cells.Length; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_cells[i] = new TableCell();
object[] m_arr = (object[])m_arrHeader[i];
m_cells[i].Text = m_arr.GetValue(0).ToString(); //赋header文字
![](/Images/OutliningIndicators/InBlock.gif)
if (m_arr.Length > 1) //如果有行列属性
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (m_arr.GetValue(1).ToString().ToLower() == "rowspan")//如果有rowspan属性
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_cells[i].RowSpan = Convert.ToInt32(m_arr.GetValue(2));
}
else if (m_arr.GetValue(1).ToString().ToLower() == "colspan")//如果有colspan属性
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_cells[i].ColumnSpan = Convert.ToInt32(m_arr.GetValue(2));
}
![](/Images/OutliningIndicators/InBlock.gif)
//处理单元格的样式
if (m_arr.Length >= 4)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (m_arr.GetValue(3) != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Hashtable m_hsStyles = (Hashtable)m_arr.GetValue(3);
if (m_hsStyles.Count > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Collections.IDictionaryEnumerator m_style = m_hsStyles.GetEnumerator();
for (int j = 0; j < m_hsStyles.Count; j++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_style.MoveNext();
m_cells[i].Style.Add(m_style.Key.ToString(),m_style.Value.ToString());
}
}
}
}//if (m_arr.Length > 4) 结束
}//if (m_arr.Length > 1) //如果有行列属性 结束
}// for (int i=0; i< m_cells.Length; i++) 结束
![](/Images/OutliningIndicators/InBlock.gif)
//将cell加入DataGridItem
foreach (TableCell c in m_cells)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_dg.Cells.Add(c);
}
![](/Images/OutliningIndicators/InBlock.gif)
//将DataGridItem加入DataGrid
if (m_grid.AllowPaging == true || m_grid.AllowCustomPaging == true)//如果有分页
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_tb.Rows.AddAt(m_iTmp + 1,m_dg);
}
else //如果没有分页(处理导出excel时)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_tb.Rows.AddAt(m_iTmp,m_dg);
}
![](/Images/OutliningIndicators/InBlock.gif)
m_iTmp++;
}//foreach (object obj in m_headers) 结束
#endregion 处理多行表头的实体
} 使用方法:
ArrayList m_headers = new ArrayList();
ArrayList m_arrHeader1 = new ArrayList();
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
m_arrHeader1.Add(new object[]
{"事业部","rowspan","2"});
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
m_arrHeader1.Add(new object[]
{"办事处","rowspan","2"});
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
m_arrHeader1.Add(new object[]
{"每月合同培训完成名额数","colspan","12"});
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
m_arrHeader1.Add(new object[]
{"合计","rowspan","2"});
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
m_arrHeader1.Add(new object[]
{"积压<br>名额数","rowspan","2"});
![](/Images/OutliningIndicators/None.gif)
ArrayList m_arrHeader2 = new ArrayList();
for (int i=0; i<12; i++)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
m_arrHeader2.Add(new object[]
{(i+1).ToString() + "月"});
}
![](/Images/OutliningIndicators/None.gif)
m_headers.Add(m_arrHeader1);
m_headers.Add(m_arrHeader2);
![](/Images/OutliningIndicators/None.gif)
this.m_BLL.SetMultiLayerHeader(this.dgrSalesList,e,m_headers);
愿一路奔跑不退缩,到目前一直从事.Net的B/S,C/S企业应用研发