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 // } /**/ /// /// 重写表头 /// /// 目标表头 /// 新表头 /// /// 等级#级别#上期结存 件数,重量,比例#本期调入 收购调入 件数,重量,比例#本期发出 车间投料 件数,重量, /// 比例#本期发出 产品外销百分比 件数,重量,比例#平均值 /// public void SplitTableHeader(GridViewRow targetHeader, string newHeaderNames) { TableCellCollection tcl = targetHeader.Cells;//获得表头元素的实例 tcl.Clear();//清除元素 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());//添加表头控件 tcl[tcl.Count - 1].RowSpan = RowSpan; tcl[tcl.Count - 1].ColumnSpan = ColSpan; tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center; tcl[tcl.Count - 1].Style["background-color"] = "#ECF5FF"; tcl[tcl.Count - 1].Style["border"] = "1px solid #000000"; if (LastFName == "步骤") { tcl[tcl.Count - 1].Text = ""; } else { tcl[tcl.Count - 1].Text = LastFName; } break; case -1: string[] EndColName = LastFName.Split(new char[] { ',' }); foreach (string eName in EndColName) { tcl.Add(new TableHeaderCell());//添加表头控件 tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center; tcl[tcl.Count - 1].Text = eName; tcl[tcl.Count - 1].Style["background-color"] = "#ECF5FF"; tcl[tcl.Count - 1].Style["border"] = "1px solid #000000"; } break; } } if (k != row - 1) {//不是起始行,加入新行标签 tcl[tcl.Count - 1].Text = tcl[tcl.Count - 1].Text + ""; } } } /**/ /// /// 如果上一行已经输出和当前内容相同的列头,则不显示 /// /// 表头集合 /// 行索引 /// 列索引 /// 1:显示,-1:含','分隔符,0:不显示 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; } /**/ /// /// 取得和当前索引行及列对应的下级的内容所跨的行数 /// /// 表头集合 /// 行数 /// 行索引 /// 列索引 /// 行数 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; } /**/ /// /// 取得和当前索引行及列对应的下级的内容所跨的列数 /// /// 表头集合 /// 行数 /// 列数 /// 行索引 /// 列索引 /// 列数 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 = ColSpan == 1 ? 0 : ColSpan; for (int i = colIndex + 1; i < col; i++) { if (ColumnList[i, rowIndex] == LastName) { if (LastName == "类别") { ColSpan += ColumnList[i, row - 1].Split(new char[] { ',' }).Length + 1; } else { ColSpan += ColumnList[i, row - 1].Split(new char[] { ',' }).Length; } } else { LastName = ColumnList[i, rowIndex]; break; } } return ColSpan; } /**/ /// /// 将已定义的表头保存到数组 /// /// 新表头 /// 行数 /// 列数 /// 表头数组 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; } /**/ /// /// 取得复合表头的行数 /// /// 新表头 /// 行数 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; } /**/ /// /// 取得复合表头的列数 /// /// 新表头 /// 列数 private int GetColCount(string newHeaders) { return newHeaders.Split(new char[] { '#' }).Length; } }