博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GridView动态生成列问题
阅读量:5926 次
发布时间:2019-06-19

本文共 31103 字,大约阅读时间需要 103 分钟。

最近做一个报表,用户需要数据根据查询条件来动态显示。在实际实现过程中,涉及以下几个问题点:

1、GridView表头(多行)动态生成

2、数据行动态生成

完成样式如下:

贴出代码:

1  public partial class TotalYield : System.Web.UI.Page  2     {  3         protected string strConnectionString;  4         protected OracleParameter[] objCommandParameters = new OracleParameter[10];  5         protected string err;  6         protected OracleConnection conn;   7         protected CommonClass.DBClass DB = new DBClass();    8           9         protected void Page_Load(object sender, EventArgs e) 10         { 11             strConnectionString = DB.Get_Connect_String("AZAIT"); 12             conn = new OracleConnection(strConnectionString); 13             conn.Open(); 14             if (!Page.IsPostBack) 15             { 16                 InitProduct(conn); 17                 LoadDateAndTime(sender, e); 18                 InitLine(); 19                 conn.Close(); 20             } 21         } 22  23         protected void imbtn_search_Click(object sender, ImageClickEventArgs e) 24         { 25             DataTable dt =new DataTable ();  26             DataTable dt_group = BindData(); 27              28             switch (ddl_yield_type.SelectedValue) 29             {  30                 case "0": 31  32                     #region[定義列] 33                     string[] strColumn = new string[] { "Station", "Goal", "Line", "Fresh_Input", "Fresh_Pass", "Fresh_Yield", "Control_Input", "Control_Pass", "Control_Yield", "Rework_Input", "Rework_Pass", "Rework_Yield", "Repair_Input", "Repair_Pass", "Repair_Yield", "Total_Input", "Total_Pass", "Total_Yield" }; 34                     foreach (string column in strColumn) 35                     { 36                         DataColumn dc = new DataColumn(column, Type.GetType("System.String")); 37                         dt.Columns.Add(dc); 38                     } 39                     #endregion  40  41                     #region[定義行] 42                     foreach (DataRow drr in dt_group.Rows) 43                     { 44                         DataRow dr = dt.NewRow(); 45  46                         string line = drr["LINE_NAME"].ToString().Trim(); 47                         string station = drr["STATION_NAME"].ToString().Trim(); 48                         DataRow[] drs_fresh = dt_group.Select(GetFilter("Fresh_Yield",line,station)); 49                         DataRow[] drs_control = dt_group.Select(GetFilter("Control_Yield", line, station)); 50                         DataRow[] drs_rework = dt_group.Select(GetFilter("Rework_Yield", line, station)); 51                         DataRow[] drs_repair = dt_group.Select(GetFilter("Repair_Yield", line, station)); 52  53                         dr[0] = station; 54                         dr[1] = drr["GOAL_YIELD"]; 55                         dr[2] = line; 56                         dr[3] = drs_fresh.Length > 0 ? drs_fresh[0]["INPUT_QTY"] : 0; 57                         dr[4] = drs_fresh.Length > 0 ? drs_fresh[0]["PASS_QTY"] : 0; 58                         dr[5] = drs_fresh.Length > 0 ? ShowYield(drs_fresh[0]["YIELD"]) : "N/A"; 59  60                         dr[6] = drs_control.Length > 0 ? drs_control[0]["INPUT_QTY"] : 0; 61                         dr[7] = drs_control.Length > 0 ? drs_control[0]["PASS_QTY"] : 0; 62                         dr[8] = drs_control.Length > 0 ? ShowYield(drs_control[0]["YIELD"]) : "N/A"; 63  64                         dr[9] = drs_rework.Length > 0 ? drs_rework[0]["INPUT_QTY"] : 0; 65                         dr[10] = drs_rework.Length > 0 ? drs_rework[0]["PASS_QTY"] : 0; 66                         dr[11] = drs_rework.Length > 0 ? ShowYield(drs_rework[0]["YIELD"]) : "N/A"; 67  68                         dr[12] = drs_repair.Length > 0 ? drs_repair[0]["INPUT_QTY"] : 0; 69                         dr[13] = drs_repair.Length > 0 ? drs_repair[0]["PASS_QTY"] : 0; 70                         dr[14] = drs_repair.Length > 0 ? ShowYield(drs_repair[0]["YIELD"]) : "N/A"; 71  72                         dr[15] = ToInt(dr[3]) + ToInt(dr[6]) + ToInt(dr[9]) + ToInt(dr[12]); 73                         dr[16] = ToInt(dr[4]) + ToInt(dr[7]) + ToInt(dr[10]) + ToInt(dr[13]); 74                         dr[17] = ComputeYield(ToInt(dr[15]),ToInt(dr[16])); 75  76                         dt.Rows.Add(dr); 77                     } 78                     #endregion 79  80                     #region [去掉重複行] 81                     dt = new DataView(dt).ToTable(true, strColumn);                    82                     #endregion 83  84                     #region[LineCumYield] 85                     if (ddlLine.SelectedItem.Text == "ALL") 86                     { 87                         DataTable dtl = GetLine(); 88                         foreach (DataRow dr in dtl.Rows) 89                         { 90                             DataRow dr_line = dt.NewRow(); 91                             dr_line[0] = "Line Cum Yield:"; 92                             dr_line[1] = "99%"; 93                             dr_line[2] = dr["NAMEE"]; 94                             dr_line[5] = GetCumYield(dr["NAMEE"].ToString(), "Fresh_Yield", "Fresh_Input", dt); 95                             dr_line[8] = GetCumYield(dr["NAMEE"].ToString(), "Control_Yield", "Control_Input", dt); 96                             dr_line[11] = GetCumYield(dr["NAMEE"].ToString(), "Rework_Yield", "Rework_Input", dt); 97                             dr_line[14] = GetCumYield(dr["NAMEE"].ToString(), "Repair_Yield", "Repair_Input", dt); 98                             double TotalYield = (ComputeTotalYield(dr_line[5]) * ComputeTotalYield(dr_line[8]) * ComputeTotalYield(dr_line[11]) * ComputeTotalYield(dr_line[14])); 99                             dr_line[17] = (dr_line[5].ToString() == "N/A" && dr_line[8].ToString() == "N/A" && dr_line[11].ToString() == "N/A" && dr_line[14].ToString() == "N/A") ? "N/A" : TotalYield.ToString("P2");100                             dt.Rows.Add(dr_line);101                         }102                     }103                     else104                     {105                         string line = ddlLine.SelectedItem.Text;106                         DataRow dr_line = dt.NewRow();107                         dr_line[0] = "Line Cum Yield:";108                         dr_line[1] = "99%";109                         dr_line[2] = line;110                         dr_line[5] = GetCumYield(line, "Fresh_Yield", "Fresh_Input", dt);111                         dr_line[8] = GetCumYield(line, "Control_Yield", "Control_Input", dt);112                         dr_line[11] = GetCumYield(line, "Rework_Yield", "Rework_Input", dt);113                         dr_line[14] = GetCumYield(line, "Repair_Yield", "Repair_Input", dt);114                         double TotalYield = (ComputeTotalYield(dr_line[5]) * ComputeTotalYield(dr_line[8]) * ComputeTotalYield(dr_line[11]) * ComputeTotalYield(dr_line[14]));115                         dr_line[17] = (dr_line[5].ToString() == "N/A" && dr_line[8].ToString() == "N/A" && dr_line[11].ToString() == "N/A" && dr_line[14].ToString() == "N/A") ? "N/A" : TotalYield.ToString("P2");116                         dt.Rows.Add(dr_line);117                     }118                     #endregion119                     BindGridView(dt, gv_data);  120                     break;121                 case "1":122                     DefineTable("Fresh_Input", "Fresh_Pass", "Fresh_Yield", dt_group);123                     break;124                 case "2":125                     DefineTable("Control_Input", "Control_Pass", "Control_Yield", dt_group);126                     break;127                 case "3":128                     DefineTable("Rework_Input", "Rework_Pass", "Rework_Yield", dt_group);129                     break;130                 case "4":131                     DefineTable("Repair_Input", "Repair_Pass", "Repair_Yield", dt_group);132                     break;133                 default:134                     break;135             }136 137         }138 139         private void DefineTable(string InputColumn, string PassColumn, string YieldColumn, DataTable DtGroup)140         {141             DataTable dt = new DataTable();142             #region[定義列]143             string[] Columns = new string[] { "Station", "Goal", "Line", InputColumn, PassColumn, YieldColumn };144             foreach (string column in Columns)145             {146                 DataColumn dc = new DataColumn(column, Type.GetType("System.String"));147                 dt.Columns.Add(dc);148             }149             #endregion150             #region[定義行]151             foreach (DataRow drr in DtGroup.Rows)152             {153                 DataRow dr = dt.NewRow();154                 string line = drr["LINE_NAME"].ToString().Trim();155                 string station = drr["STATION_NAME"].ToString().Trim();156                 DataRow[] drs = DtGroup.Select(GetFilter(YieldColumn, line, station));157                 dr[0] = station;158                 dr[1] = drr["GOAL_YIELD"];159                 dr[2] = line;160                 dr[3] = drs.Length > 0 ? drs[0]["INPUT_QTY"] : 0;161                 dr[4] = drs.Length > 0 ? drs[0]["PASS_QTY"] : 0;162                 dr[5] = drs.Length > 0 ? ShowYield(drs[0]["YIELD"]) : "N/A";163                 dt.Rows.Add(dr);164             }           165             #endregion166             dt = new DataView(dt).ToTable(true, Columns);167             #region[LineCumYield]168             if (ddlLine.SelectedItem.Text == "ALL")169             {170                 DataTable dt_line = GetLine();171                 foreach (DataRow dr in dt_line.Rows)172                 {173                     DataRow dr_line = dt.NewRow();174                     dr_line[0] = "Line Cum Yield:";175                     dr_line[1] = "99%";176                     dr_line[2] = dr["NAMEE"];177                     dr_line[5] = GetCumYield(dr["NAMEE"].ToString(), YieldColumn, InputColumn, dt);178                     dt.Rows.Add(dr_line);179                 }180             }181             else182             {183                 string line = ddlLine.SelectedItem.Text;184                 DataRow dr_line = dt.NewRow();185                 dr_line[0] = "Line Cum Yield:";186                 dr_line[1] = "99%";187                 dr_line[2] = line;188                 dr_line[5] = GetCumYield(line, YieldColumn, InputColumn, dt);189                 dt.Rows.Add(dr_line);190             }191             #endregion192             BindGridView(dt, gv_data);193         }194 195         private void BindGridView(DataTable dt,GridView gv)196         {197             gv_data.Columns.Clear();198             for (int i = 0; i < dt.Columns.Count; i++)199             {200                 BoundField bfColumn = new BoundField();201                 bfColumn.DataField = dt.Columns[i].ColumnName;202                 //bfColumn.HeaderText = dt.Columns[i].Caption;203                 gv_data.Columns.Add(bfColumn);204             }205             gv_data.DataSource = dt;206             gv_data.DataBind();207         }208 209         private string GetFilter(string YieldType,string line,string station)210         {211             string filter="";212             switch (YieldType)213             {214                 case "Fresh_Yield":215                     filter = "is_msr=0 and is_rework=0 and is_repair=0 and line_name='" + line + "' and station_name='" + station + "'";216                     break;217                 case "Control_Yield":218                     filter = "is_msr=1 and is_rework =0 and is_repair=0 and line_name='" + line + "' and station_name='" + station + "'";219                     break;220                 case "Rework_Yield":221                     filter = "is_rework =1 and line_name='" + line + "' and station_name='" + station + "' ";222                     break;223                 case "Repair_Yield":224                     filter = "is_msr=0 and is_rework =0 and is_repair=1 and line_name='" + line + "' and station_name='" + station + "' ";225                     break;226                 default:227                     break;228             }229             return filter;230         }231 232         private string GetCumYield(string line,string YieldTypeColumn,string YieldInput,DataTable dt)233         {234             int TotalInput = 0;235             DataRow[] drs = dt.Select("line='"+line+"'");236             double yield = 1.00;237             if (drs.Length > 0)238             {239                 foreach (DataRow dr in drs)240                 {241                     if (dr[YieldTypeColumn].ToString() != "N/A" && dr[YieldTypeColumn].ToString() != "0")242                     {243                         yield *= (Convert.ToDouble(dr[YieldTypeColumn].ToString().Replace("%", ""))) / 100;244                     }245                     if (Convert.ToInt16(dr[YieldInput]) != 0)246                     {247                         TotalInput += Convert.ToInt16(dr[YieldInput]);248                     }249                 }250                 if (TotalInput == 0)251                 {252                     return "N/A";253                 }254                 else255                 {256                     return yield.ToString("P2");257                 }258             }259             else260             {261                 return "N/A";262             }263         }264 265         protected void imbtn_excel_Click(object sender, ImageClickEventArgs e)266         {267             Response.Clear();268             Response.Buffer = true;269             Response.Charset = "utf-8";270             Response.AppendHeader("Content-Disposition", "attachment;filename=TotalYiedReport.xls");271             Response.ContentEncoding = System.Text.Encoding.GetEncoding("Big5");272             //System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-TW", true);273             Response.ContentType = "application/ms-excel";274             this.EnableViewState = false;275             StringWriter sWrite = new StringWriter();276             HtmlTextWriter txtWrite = new HtmlTextWriter(sWrite);277             gv_data.RenderControl(txtWrite);278             Response.Write(sWrite.ToString());279             Response.End();280         }281         public override void VerifyRenderingInServerForm(Control control)282         {283 284         }285         protected void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)286         {287             InitLine();288         }289 290         private DataTable BindData()291         {292             DataTable dt = new DataTable();293             #region OracleParameter[]294             OracleParameter[] OraParam = new OracleParameter[9];295             OraParam[0] = new OracleParameter("V_PRODUCT_NAME", OracleType.VarChar, 20);296             OraParam[0].Value = ddlProduct.SelectedValue;297             OraParam[1] = new OracleParameter("V_LINE_CODE", OracleType.VarChar, 20);298             OraParam[1].Value = ddlLine.SelectedValue;299             OraParam[2] = new OracleParameter("V_WO_NO", OracleType.VarChar, 20);300             OraParam[2].Value = txtWO.Text.Trim();301             OraParam[3] = new OracleParameter("V_SKU_NO", OracleType.VarChar, 20);302             OraParam[3].Value = txtSku.Text.Trim();303             OraParam[4] = new OracleParameter("V_START_DATE", OracleType.VarChar, 10);304             OraParam[4].Value = txtDF.Text.Trim();305             OraParam[5] = new OracleParameter("V_END_DATE", OracleType.VarChar, 10);306             OraParam[5].Value = txtDT.Text.Trim();307             OraParam[6] = new OracleParameter("V_START_TIME", OracleType.VarChar, 10);308             OraParam[6].Value = ddlTF.SelectedValue;309             OraParam[7] = new OracleParameter("V_END_TIME", OracleType.VarChar, 10);310             OraParam[7].Value = ddlTT.SelectedValue;           311             OraParam[8] = new OracleParameter("P_CURSOR", OracleType.Cursor);312             OraParam[8].Direction = ParameterDirection.Output;313             #endregion314 315             dt = DB.Execute_Return_DataTable("AZAIT", CommandType.StoredProcedure, "RT_AIT_DAILY_YIELD_ALL_PK.ALL_DAILY_YIELD_SP", OraParam, "BU");316 317             return dt;318         }319 320         #region Inition Query Condition Include Product321 322         private void InitProduct(OracleConnection DBConn)323         {324             OracleParameter[] objCommandParameters = new OracleParameter[2];325 326             objCommandParameters[0] = new OracleParameter("RES", OracleType.VarChar, 20);327             objCommandParameters[0].Direction = ParameterDirection.Output;328             objCommandParameters[1] = new OracleParameter("P_CURSOR", OracleType.Cursor);329             objCommandParameters[1].Direction = ParameterDirection.Output;330             this.ddlProduct.Items.Clear();331 332             foreach (DataRow dr in DB.Execute_Return_DataTable(CommandType.StoredProcedure,333                 "RT_AIT_BASE_SEL_PK.GET_PRODUCT_SP", objCommandParameters, DBConn).Rows)334                 this.ddlProduct.Items.Add(new ListItem(dr[1].ToString(), dr[1].ToString()));335         }336         private void LoadDateAndTime(object sender, System.EventArgs e)337         {338             this.txtDF.Text = DateTime.Today.ToString("yyyy-MM-dd");339             this.txtDT.Text = DateTime.Today.ToString("yyyy-MM-dd");340             GetTurnTime("AZAIT", DateTime.Today.ToString("yyyy-MM-dd"));341             ddlTurnTimes_SelectedIndexChanged(sender, e);342             //txtDF.Attributes.Add("onpropertychange", "txtDFChange()");  343         }344         private void InitTime(string strConn, string turnDate)345         {346             objCommandParameters[0] = new OracleParameter("V_TURN_DATE", OracleType.VarChar, 20);347             objCommandParameters[0].Value = turnDate;348             objCommandParameters[1] = new OracleParameter("RES", OracleType.VarChar, 20);349             objCommandParameters[1].Direction = ParameterDirection.Output;350             objCommandParameters[2] = new OracleParameter("P_CURSOR", OracleType.Cursor);351             objCommandParameters[2].Direction = ParameterDirection.Output;352             this.ddlTF.Items.Clear();353             this.ddlTT.Items.Clear();354             foreach (DataRow dr in DB.Execute_Return_DataTable(strConn, CommandType.StoredProcedure,355                                                             "RT_AIT_BASE_SEL_PK.GET_WORK_TIME_BY_TURNDATE_SP", objCommandParameters, "BU").Rows)356             {357                 this.ddlTF.Items.Add(new ListItem(dr[1].ToString(), dr[1].ToString()));358                 this.ddlTT.Items.Add(new ListItem(dr[2].ToString(), dr[2].ToString()));359             }360             ddlTF.SelectedIndex = 0;361             ddlTT.SelectedIndex = 10;362         }363 364         private void GetTurnTime(string strConn, string startDate)365         {366             objCommandParameters[0] = new OracleParameter("V_START_DATE", OracleType.VarChar, 20);367             objCommandParameters[0].Value = startDate;368             objCommandParameters[1] = new OracleParameter("RES", OracleType.VarChar, 20);369             objCommandParameters[1].Direction = ParameterDirection.Output;370             objCommandParameters[2] = new OracleParameter("P_CURSOR", OracleType.Cursor);371             objCommandParameters[2].Direction = ParameterDirection.Output;372             ddlTurnTimes.Items.Clear();373 374             foreach (DataRow dr in DB.Execute_Return_DataTable(strConn, CommandType.StoredProcedure,375                                                             "RT_AIT_BASE_SEL_PK.GET_TURN_TIMES_SP", objCommandParameters, "BU").Rows)376             {377                 this.ddlTurnTimes.Items.Add(new ListItem(dr[1].ToString(), dr[0].ToString()));378             }379             ddlTurnTimes.SelectedIndex = 0;380             InitTime("AZAIT", ddlTurnTimes.SelectedValue);381         }382 383         protected void txtDF_TextChanged(object sender, EventArgs e)384         {385             try386             {387                 GetTurnTime("AZAIT", txtDF.Text);388                 InitLine();389             }390             catch391             { }392         }393 394         protected void txtDT_TextChanged(object sender, EventArgs e)395         {396             try397             {398                 //GetTurnTime("AZAIT", txtDF.Text);399                 InitLine();400             }401             catch402             { }403         }404 405 406         protected void ddlTurnTimes_SelectedIndexChanged(object sender, EventArgs e)407         {408             InitTime("AZAIT", ddlTurnTimes.SelectedValue);409         }410 411         private void InitLine()412         {413             DataTable dt = GetLine();414             ddlLine.Items.Clear();415             ddlLine.Items.Add(new ListItem("ALL", ""));416             foreach (DataRow dr in dt.Rows)417             {418                 this.ddlLine.Items.Add(new ListItem(dr[1].ToString(), dr[2].ToString()));419             }420            421         }422 423 424         private DataTable GetLine()425         {426             strConnectionString = DB.Get_Connect_String("AZAIT");427             conn = new OracleConnection(strConnectionString);            428             if (conn.State == ConnectionState.Closed)429             { conn.Open(); }430            431             DataTable dt = new DataTable();            432             objCommandParameters[0] = new OracleParameter("V_MODEL", OracleType.VarChar, 20);433             objCommandParameters[0].Value = ddlProduct.SelectedValue;434             objCommandParameters[1] = new OracleParameter("V_DATE_FROM", OracleType.VarChar, 20);435             objCommandParameters[1].Value = txtDF.Text + " " + ddlTF.SelectedValue;436             objCommandParameters[2] = new OracleParameter("V_DATE_TO", OracleType.VarChar, 20);437             objCommandParameters[2].Value = txtDT.Text + " " + ddlTT.SelectedValue;438             objCommandParameters[3] = new OracleParameter("RES", OracleType.VarChar, 20);439             objCommandParameters[3].Direction = ParameterDirection.Output;440             objCommandParameters[4] = new OracleParameter("p_cursor", OracleType.Cursor);441             objCommandParameters[4].Direction = ParameterDirection.Output;442 443             dt = DB.Execute_Return_DataTable(CommandType.StoredProcedure,444                 "RT_AIT_BASE_SEL_PK.GET_LINE_NEW_SP", objCommandParameters, conn);445             if (conn.State == ConnectionState.Open)446             { conn.Close(); }447             return dt;448         }449         #endregion450         451         private int ToInt(object obj)452         { 453             int res;454             if (int.TryParse(obj.ToString().Trim(), out res))455             {456                 return res;457             }458             else459             {460                 return 0;461             }462         }463 464         private string ComputeYield(int i,int j)465         {466             if (i == 0)467             {468                 return "N/A";469             }470             else471             {472                 double yield=(double)j / i;473                 if (yield < 0.99)474                 {475                     return yield.ToString("P2");476                     //return ""+yield.ToString("P2")+"";477                 }478                 else479                 {480                     return yield.ToString("P2");481                 }482             }483         }484 485         private double ComputeTotalYield(object obj)486         {487             double res;488             double yield = 1.00;489             string ores=obj.ToString().Replace("%","").Trim();490             if (ores == "N/A" || ores == "100" || ores == "0" || ores == "")491             {492                 return 1.00;493             }494             if (double.TryParse(ores, out res))495             {496                 yield *= res/100;497             }498             return yield;499 500 501         }502 503         private string ShowYield(object obj)504         {505             double res;506 507             if (double.TryParse(obj.ToString(), out res))508             {509                 if (res < 99)510                 {511                     return (res/100).ToString("P2") ;512                 }513                 else514                 {515                     return (res/100).ToString("P2");516                 }517             }518             else519             {520                 return "N/A";521             }522             523         }524 525         protected void gv_data_RowDataBound(object sender, GridViewRowEventArgs e)526         {527             if (e.Row.RowType == DataControlRowType.DataRow)528             {529                 FormatCell(e.Row.Cells);530                 //e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ccc';");531                 //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");532             }           533         }534 535         private void CreateTableHeader(TableCellCollection cells, string YieldType,int RowIndex)536         {537             538             string[] strColumns = new string[] { "Station","Goal","Line","Fresh", "Control", "Rework", "Repair", "Total" };539             switch (YieldType)540             {541                 case "0":542                     if (RowIndex == 1)543                     {544                         for (int i = 0; i < strColumns.Length; i++)545                         {546                             cells.Add(new TableHeaderCell());547                             if (i == strColumns.Length - 1)548                             {549                                 cells[i].Text = strColumns[i] + "";550                             }551                             else552                             {553                                 cells[i].Text = strColumns[i];554                             }555                             if (i == 0 || i == 1 || i == 2)556                             {557                                558                                 cells[i].Attributes.Add("rowspan", "2");559                             }560                             else561                             {562                                 cells[i].Attributes.Add("colspan", "3");563                             }564                         }565                     }566                     else if (RowIndex == 2)567                     {568                        569                         for (int i = 8; i < 21; i=i+3) 570                         {571                             cells.Add(new TableHeaderCell());572                             cells[i].Text = "Input";573                             cells.Add(new TableHeaderCell());574                             cells[i + 1].Text = "Pass";575                             cells.Add(new TableHeaderCell());576                             cells[i + 2].Text = "Yield"; 577                             578                         }579                     }580                     break;581                 case "1":582                 case "2":583                 case "3":584                 case "4":585                     if (RowIndex == 1)586                     {587                         for (int i = 0; i < 4; i++)588                         {589                             cells.Add(new TableHeaderCell());  590                             if (i == 3)591                             {592                                 cells[i].Text = strColumns[int.Parse(YieldType) + 2] + ""; 593                                 cells[i].Attributes.Add("colspan", "3");                                594                             }595                             else596                             {597                                 cells[i].Text = strColumns[i];598                                 cells[i].Attributes.Add("rowspan", "2");                                599                             }600                         }601                     }602                     else if (RowIndex == 2)603                     {604                         cells.Add(new TableHeaderCell());  605                         cells[4].Text = "Input";606                         cells.Add(new TableHeaderCell());  607                         cells[5].Text = "Pass";608                         cells.Add(new TableHeaderCell());  609                         cells[6].Text = "Yield";610                     }611                     break;612                 default:613                     break;614             }615         }616        617         private void FormatCell(TableCellCollection cell )618         {619             List
ColumnIndex=new List
();620 switch (ddl_yield_type.SelectedValue)621 {622 case "0": 623 ColumnIndex.Add(5);624 ColumnIndex.Add(8);625 ColumnIndex.Add(11);626 ColumnIndex.Add(14);627 ColumnIndex.Add(17);628 break;629 case "1":630 case "2":631 case "3":632 case "4":633 ColumnIndex.Add(5); 634 break; 635 default:636 break;637 }638 double res;639 foreach(int index in ColumnIndex)640 {641 if(double.TryParse((cell[index].Text).Replace("%","").ToString(),out res))642 {643 if (res<99)644 {645 cell[index].Style.Value = "Background-Color:red";646 }647 }648 }649 650 }651 652 protected void gv_data_PreRender(object sender, EventArgs e)653 {654 GridDecorator.MergeRows(gv_data);655 }656 657 protected void gv_data_RowCreated(object sender, GridViewRowEventArgs e)658 {659 if (e.Row.RowType == DataControlRowType.Header)660 {661 TableCellCollection tcHeader = e.Row.Cells;662 tcHeader.Clear();663 //添加第一行表頭664 CreateTableHeader(tcHeader, ddl_yield_type.SelectedValue, 1);665 //添加第二行表頭666 CreateTableHeader(tcHeader, ddl_yield_type.SelectedValue, 2);667 }668 }669 670 }
View Code

问题点:

1、动态生成列的时候,每次绑定数据源都要清空现有列——gv_data.Columns.Clear();否则,实际数据显示时会累加各列。

2、动态生成表头的时候,函数要写在RowCreated里,且表头集合也要清空。TableCellCollection tcHeader = e.Row.Cells;tcHeader.Clear();否则,会造成表头错位。

3、表头若有多行,在每行的最后一个单元格末尾,要加上换行标记</th></tr><tr>。否则,表头会一直横向排列,无限扩展。

 

转载于:https://www.cnblogs.com/pushaoxia/p/3247704.html

你可能感兴趣的文章
java线程安全问题原因及解决办法
查看>>
[转]经典SQL语句大全
查看>>
Python之路【第一篇】:环境搭建
查看>>
html+css常用小笔记(持续更新)
查看>>
Jenkins入门系列之——03PDF文档下载
查看>>
Winform VS2015打包
查看>>
第二次装OA系统
查看>>
R升级和包更新
查看>>
API之子窗口创建 (转)
查看>>
JavaScript 使用random()生成随机数
查看>>
基于Azure Blob冷存储的数据压缩备份总结
查看>>
java总结
查看>>
SublimeText 自带格式化代码功能
查看>>
ThreadLocal源码剖析
查看>>
linux每日命令(5):mkdir命令
查看>>
uva-10305-水题-拓扑排序
查看>>
ArrayList实现
查看>>
西湖论剑WP
查看>>
Openfire3.10beta版源码在eclipse上部署编译
查看>>
如何线性求乘法逆元
查看>>