DataTable dtTmp { get { if (ViewState["dtTmp"] == null) { return null; } return ViewState["dtTmp"] as DataTable; } set { ViewState["dtTmp"] = value; } } private void GridBind() { //BLL層調用DAL層 function() dtTmp = NoPayManager.AccessObject.SlctDate(); GridNoPay.DataSource = dtTmp; GridNoPay.DataBind(); } #region 產生GirdView空白表頭 protected void GridNoPay_PreRender(object sender, EventArgs e) { if (dtTmp != null) { if (dtTmp.Rows.Count == 0 || this.GridNoPay.Rows.Count == 0) { renderEmptyGridView(this.GridNoPay, DAL.NoPayDao.GetRenderEmptyGridViewColumnNames(this.dtTmp)); } } } /// <summary> /// 產生空白表頭 /// </summary> /// <param name="GridView"></param> /// <param name="ColumnNames"></param>1 public static void renderEmptyGridView(GridView GridView, string ColumnNames) { try { DataTable dt = new DataTable(); char[] separator = { ',' }; string[] columnNames = ColumnNames.Split(separator); #region " 空白表頭" //GridView無資料時,新增DataColumn foreach (string columnName in columnNames) { DataColumn dataColumn = new DataColumn(columnName.Trim()); dt.Columns.Add(dataColumn); } //GridView無資料時,新增DataRow DataRow DataRow = dt.NewRow(); foreach (string columnName in columnNames) { DataRow[columnName.Trim()] = System.DBNull.Value; } dt.Rows.Add(DataRow); #endregion GridView.DataSourceID = null; GridView.DataSource = dt; GridView.DataBind(); GridView.Rows[0].Visible = false; } catch (Exception ex) { throw ex; } } #endregion //DAL: public static readonly NoPayDao AccessObject = new NoPayDao(); // string SqlCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; public static string GetRenderEmptyGridViewColumnNames(DataTable DT) { try { string data = string.Empty; for (int i = 0; i < DT.Columns.Count; i++) { data += (i == 0 ? string.Empty : ","); data += DT.Columns[i].ColumnName; } return data; } catch (Exception ex) { throw ex; } } public DataTable SlctDate() { string sql = @"select t.leaveid, t.leavename, t.status, to_char(t.starttime,'yyyy/mm/dd') starttime, to_char(t.endtime,'yyyy/mm/dd') endtime, t.updateby,t.updatetime from lookup t ORDER BY t.leaveid"; try { // DataSet ds = new DataSet(); DataSet ds = OracleHelper.Query(sql, "Pay"); //if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0) //{ return ds.Tables[0]; //} //else //{ // return null; //} } catch { throw new Exception(); } }