2009-03-24 [ASP.NET][GridView] GridViewRow內建置Detail GridView (GridViewRow inside the building at Detail GridView) 7048 0 ASP.NET 2009-09-02 摘要:[ASP.NET]GridViewRow內建置DetailGridView //在Row資料繫結時觸發事件 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { gc.FootRowInsertTotalRowsString(e.Row.RowType, DataControlRowType.Pager, e.Row, intTotalRows); if (Page.IsPostBack) { //ViewState["PLMSP03_21"]為DetailGridView查詢資料之參數 if (ViewState["PLMSP03_21"].ToString() != "") { //假如事件e的列型別絕對等於資料列(資料控制項列型別) if (e.Row.RowType == DataControlRowType.DataRow) { //事件e的Row轉為DataRowView DataRowView drv = (DataRowView)e.Row.DataItem; String strPLMSP03_21 = ViewState["PLMSP03_21"].ToString(); //判斷訂單編號相同的Row才要處理 if (drv.Row["物料編號"].ToString() == strPLMSP03_21) { DataTable odt = new DataTable(); //資料連線設定 using (OracleConnection oCon = new OracleConnection(ConfigurationManager.ConnectionStrings["SPEC2008DSW"].ToString())) { //SQL字串 StringBuilder SqlTxt = new StringBuilder(); SqlTxt.Append(@" SELECT PLMSP03_06 ""註記"", "); SqlTxt.Append(@" PLMSP04_01 ""樣品訂單號碼"", "); SqlTxt.Append(@" PLMPU05_01 ""開發目的"", "); SqlTxt.Append(@" PLMSP03_20 ""顏色"", "); SqlTxt.Append(@" PLMSP04_09 ""需求日期"" "); SqlTxt.Append(@" FROM PLMSP03,PLMSP04,PLMPU05 "); SqlTxt.Append(@" WHERE PLMSP03_F1 = PLMSP04_F2 AND "); SqlTxt.Append(@" PLMSP04_05 = PLMPU05_PK AND "); SqlTxt.Append(@" PLMSP04_AC = 'Y' AND "); SqlTxt.Append(@" PLMSP03_21 = :PLMSP03_21 "); SqlTxt.Append(@" GROUP BY PLMSP03_06, PLMSP04_01, PLMPU05_01, PLMSP03_20, PLMSP04_09 "); //建立SQL指令並指定連線 OracleCommand oCmd = new OracleCommand(SqlTxt.ToString(), oCon); //設定SQL參數 oCmmd.Parameters.AddWithValue("PLMSP03_21", ViewState["PLMSP03_21"]); //建立資料配接器並指定SQL指令 OracleDataAdapter oda = new OracleDataAdapter(oCmd); //使用配接器將資料填入DataTable odt oda.Fill(odt); } //產生新的GridViewRow(DataRow) GridViewRow gvRow = new GridViewRow(0, -1, DataControlRowType.DataRow, DataControlRowState.Normal); //建立注釋說明儲存格 *-開發 P-量產 S-銷樣 TableCell tCellF = new TableCell(); //注釋說明儲存格 文字內容 tCellF.Text = "*-開發" + Environment.NewLine + "P-量產" + Environment.NewLine + "S-銷樣"; //注釋說明儲存格 垂直置上 tCellF.VerticalAlign = VerticalAlign.Top; tCellF.Font.Size = 8; //將Cell放入DataRow內 gvRow.Cells.Add(tCellF); //建立欲放置DetailGridView的儲存格 TableCell tCell = new TableCell(); if (odt.Rows.Count > 0) { //如果DataTable有資料(列) //建立DetailGridView GridView gvDetail = new GridView(); //指定資料來源為 odt (DataTable) gvDetail.DataSource = odt; //設定自動產生欄位 gvDetail.AutoGenerateColumns = true; //設定DetailGridView背景顏色 gvDetail.BackColor = System.Drawing.Color.LightGray; //資料繫結 gvDetail.DataBind(); //將gvDetail放入Cell內 tCell.Controls.Add(gvDetail); } else { //如果DataTable沒有資料(列) //建立Label Label lb = new Label(); //設定Label背景顏色 lb.BackColor = System.Drawing.Color.LightGray; //Label文字欄欄位 lb.Text = "物料編號[" + strPLMSP03_21 + "]目前沒有訂單使用!"; //將Label放入Cell tCell.Controls.Add(lb); } //設定合併Cell欄位數 tCell.Attributes.Add("colspan", "9"); //設定Cell置左 tCell.Attributes.Add("align", "Left"); //Cell放入GridViewRow gvRow.Cells.Add(tCell); //設定DataRow背景顏色 gvRow.BackColor = System.Drawing.Color.LightCyan; //GridViewRow放到GridView中 e.Row.Parent.Controls.AddAt(e.Row.RowIndex + 2, gvRow); } } } } } C#ASP.NETGridViewDetailGridViewMasterGridView 回首頁