/// <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)
{
e.Item.Visible = false;
//处理特殊情况
if (m_grid.Controls.Count <= 0)
{
return;
}
Table m_tb = (Table)m_grid.Controls[0];
int m_iTmp = 0;
#region 处理多行表头的实体
foreach (object obj in m_headers)
{
DataGridItem m_dg = new DataGridItem(0,0,ListItemType.Header);
ArrayList m_arrHeader = (ArrayList)obj;
TableCell[] m_cells = new TableCell[m_arrHeader.Count];
for (int i=0; i< m_cells.Length; i++)
{
m_cells[i] = new TableCell();
object[] m_arr = (object[])m_arrHeader[i];
m_cells[i].Text = m_arr.GetValue(0).ToString(); //赋header文字
if (m_arr.Length > 1) //如果有行列属性
{
if (m_arr.GetValue(1).ToString().ToLower() == "rowspan")//如果有rowspan属性
{
m_cells[i].RowSpan = Convert.ToInt32(m_arr.GetValue(2));
}
else if (m_arr.GetValue(1).ToString().ToLower() == "colspan")//如果有colspan属性
{
m_cells[i].ColumnSpan = Convert.ToInt32(m_arr.GetValue(2));
}
//处理单元格的样式
if (m_arr.Length >= 4)
{
if (m_arr.GetValue(3) != null)
{
Hashtable m_hsStyles = (Hashtable)m_arr.GetValue(3);
if (m_hsStyles.Count > 0)
{
System.Collections.IDictionaryEnumerator m_style = m_hsStyles.GetEnumerator();
for (int j = 0; j < m_hsStyles.Count; j++)
{
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++) 结束
//将cell加入DataGridItem
foreach (TableCell c in m_cells)
{
m_dg.Cells.Add(c);
}
//将DataGridItem加入DataGrid
if (m_grid.AllowPaging == true || m_grid.AllowCustomPaging == true)//如果有分页
{
m_tb.Rows.AddAt(m_iTmp + 1,m_dg);
}
else //如果没有分页(处理导出excel时)
{
m_tb.Rows.AddAt(m_iTmp,m_dg);
}
m_iTmp++;
}//foreach (object obj in m_headers) 结束
#endregion 处理多行表头的实体
}
/// 方法编号: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)
{
e.Item.Visible = false;
//处理特殊情况
if (m_grid.Controls.Count <= 0)
{
return;
}
Table m_tb = (Table)m_grid.Controls[0];
int m_iTmp = 0;
#region 处理多行表头的实体
foreach (object obj in m_headers)
{
DataGridItem m_dg = new DataGridItem(0,0,ListItemType.Header);
ArrayList m_arrHeader = (ArrayList)obj;
TableCell[] m_cells = new TableCell[m_arrHeader.Count];
for (int i=0; i< m_cells.Length; i++)
{
m_cells[i] = new TableCell();
object[] m_arr = (object[])m_arrHeader[i];
m_cells[i].Text = m_arr.GetValue(0).ToString(); //赋header文字
if (m_arr.Length > 1) //如果有行列属性
{
if (m_arr.GetValue(1).ToString().ToLower() == "rowspan")//如果有rowspan属性
{
m_cells[i].RowSpan = Convert.ToInt32(m_arr.GetValue(2));
}
else if (m_arr.GetValue(1).ToString().ToLower() == "colspan")//如果有colspan属性
{
m_cells[i].ColumnSpan = Convert.ToInt32(m_arr.GetValue(2));
}
//处理单元格的样式
if (m_arr.Length >= 4)
{
if (m_arr.GetValue(3) != null)
{
Hashtable m_hsStyles = (Hashtable)m_arr.GetValue(3);
if (m_hsStyles.Count > 0)
{
System.Collections.IDictionaryEnumerator m_style = m_hsStyles.GetEnumerator();
for (int j = 0; j < m_hsStyles.Count; j++)
{
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++) 结束
//将cell加入DataGridItem
foreach (TableCell c in m_cells)
{
m_dg.Cells.Add(c);
}
//将DataGridItem加入DataGrid
if (m_grid.AllowPaging == true || m_grid.AllowCustomPaging == true)//如果有分页
{
m_tb.Rows.AddAt(m_iTmp + 1,m_dg);
}
else //如果没有分页(处理导出excel时)
{
m_tb.Rows.AddAt(m_iTmp,m_dg);
}
m_iTmp++;
}//foreach (object obj in m_headers) 结束
#endregion 处理多行表头的实体
}
使用方法:
ArrayList m_headers = new ArrayList();
ArrayList m_arrHeader1 = new ArrayList();
m_arrHeader1.Add(new object[]{"事业部","rowspan","2"});
m_arrHeader1.Add(new object[]{"办事处","rowspan","2"});
m_arrHeader1.Add(new object[]{"每月合同培训完成名额数","colspan","12"});
m_arrHeader1.Add(new object[]{"合计","rowspan","2"});
m_arrHeader1.Add(new object[]{"积压<br>名额数","rowspan","2"});
ArrayList m_arrHeader2 = new ArrayList();
for (int i=0; i<12; i++)
{
m_arrHeader2.Add(new object[]{(i+1).ToString() + "月"});
}
m_headers.Add(m_arrHeader1);
m_headers.Add(m_arrHeader2);
this.m_BLL.SetMultiLayerHeader(this.dgrSalesList,e,m_headers);
ArrayList m_arrHeader1 = new ArrayList();
m_arrHeader1.Add(new object[]{"事业部","rowspan","2"});
m_arrHeader1.Add(new object[]{"办事处","rowspan","2"});
m_arrHeader1.Add(new object[]{"每月合同培训完成名额数","colspan","12"});
m_arrHeader1.Add(new object[]{"合计","rowspan","2"});
m_arrHeader1.Add(new object[]{"积压<br>名额数","rowspan","2"});
ArrayList m_arrHeader2 = new ArrayList();
for (int i=0; i<12; i++)
{
m_arrHeader2.Add(new object[]{(i+1).ToString() + "月"});
}
m_headers.Add(m_arrHeader1);
m_headers.Add(m_arrHeader2);
this.m_BLL.SetMultiLayerHeader(this.dgrSalesList,e,m_headers);