[ASP.NET] Gridview動態增加欄位與Null值處理
.CS
//... StringBuilder strSql = new StringBuilder(string.Empty); strSql.Append("usp_UserReportDownLoad"); SqlCommand cmd = new SqlCommand(strSql.ToString()); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@BDate", BDate); cmd.Parameters.AddWithValue("@EDate", EDate); DataTable dt = DB.GetDataTable(cmd); //依據回傳的DataTable,動態增加欄位。 //第一欄固定,所以從第2欄開始 for (int i = 1; i < dt.Columns.Count; i++) { BoundField oBF = new BoundField(); oBF.DataField = dt.Columns[i].ColumnName; oBF.HeaderText = dt.Columns[i].ColumnName; oBF.NullDisplayText = "0"; //NULL值處理 this.GridView1.Columns.Add(oBF); } this.GridView1.DataSource = dt; this.GridView1.DataBind();.ASPX
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" onrowdatabound="GridView1_RowDataBound" ShowFooter="true" > <PagerSettings Visible="False" Mode="NumericFirstLast" /> <Columns> </Columns> <EmptyDataTemplate> 目前尚無資料! </EmptyDataTemplate> </asp:GridView>
留言
張貼留言