摘要:[Asp.net] GridView 的 DataBind
在寫 *.aspx 時,
蠻常會用到像是 asp:GridView , asp:DropDownList 都有 DataBind 的方式
目前常用到的幾種手法,
就先整理記下來
如果以後有需要的話,
可以幫助我回復一下記憶
===== 我是分隔線 =====
第一種, 從 aspx 中做 SQL 查詢, 得到結果後, 直接 Bind 上來
<asp:SqlDataSource ID="NSP_GameWeb_A_App_List" ConnectionString='<%$ ConnectionStrings:DBConnectionString %>'
runat="server" SelectCommand="NSP_AgentWeb_A_App_List" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Name="AppGroupNo" DbType="Int32" ControlID="ddl_AppGroup"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" SkinID="TBGridViewSkin" Width="100%"
AutoGenerateColumns="false" DataSourceID="NSP_GameWeb_A_App_List" runat="server">
<Columns>
...
</Columns>
</asp:GridView>
第二種, 由程式碼去給予, 程式碼去讀到SP之後, 再 DataBind() 上去
[a.aspx]
<asp:DropDownList ID="ddl_AppGroup" DataTextField="AppGroupName"
DataValueField="AppGroupNo" AutoPostBack="true" AppendDataBoundItems="true" CssClass="form-control" runat="server">
<asp:ListItem Value="-1">全部</asp:ListItem>
</asp:DropDownList>
[a.aspx.cs]
ddl_AppGroup.DataSource = SqlHelper.ExecuteDataset(
WebConfig.ConnectionString,
CommandType.StoredProcedure,
"NSP_AgentWeb_A_AppGroup_List"
, new SqlParameter("@AgentID", this.AUser.AgentID)
).Tables[0];
ddl_AppGroup.DataBind();
第三種, 如果有資料內容不是從 SP 就可以取得, 或是根本就沒有使用 SP 的話, 那麼也可以自己產生 DataTable , 把資料塞好後再 DataBind () 上去
public static DataTable GetDataTable(Dictionary<string, string> dictInfo)
{
DataColumn Column;
DataTable Tables = new DataTable();
foreach (var KeyValue in dictInfo)
{
// 加入欄
Column = new DataColumn();
Column.DataType = System.Type.GetType(KeyValue.Value);
Column.ColumnName = KeyValue.Key;
Tables.Columns.Add(Column);
}
return Tables;
}
DataTable DBdt = null;
// 取得所有的遊戲
DBdt = SqlHelper.ExecuteDataset(
WebConfig.ConnectionString,
CommandType.StoredProcedure,
"NSP_AgentWeb_A_AppGroup_GameAreaType_List"
, new SqlParameter("@AppGroupNo", AppGroupNo)
, new SqlParameter("@AgentID", this.AUser.AgentID)
).Tables[0];
DataTable Tables = Utility.GetDataTable (new Dictionary<string,string> ()
{
{"AppGroupNo", "System.Int32"}
, {"AppGroupName", "System.String"}
, {"AppGroupNo_JPList", "System.String"}
});
Dictionary<string, string> dictMap = new Dictionary<string, string>();
for (int Index = 0; Index < DBdt.Rows.Count; Index++)
{
DataRow NewRow = Tables.NewRow();
NewRow["AppGroupNo"] = DBdt.Rows[Index]["AppGroupNo"];
string strAppGroupNo = DBdt.Rows[Index]["AppGroupNo"].ToString();
NewRow["AppGroupName"] = DBdt.Rows[Index]["AppGroupName"];
NewRow["AppGroupNo_JPList"] = "";
if (dictMap.ContainsKey(strAppGroupNo) == false)
{
Tables.Rows.Add(NewRow);
dictMap[strAppGroupNo] = "";
}
dictMap[strAppGroupNo] += string.Format(",[{0}] {1}", DBdt.Rows[Index]["GameAreaType"], DBdt.Rows[Index]["Value_ZHTW"]);
}
for (int Index = 0; Index < Tables.Rows.Count; Index++)
{
string strAppGroupNo = Tables.Rows[Index]["AppGroupNo"].ToString();
Tables.Rows[Index]["AppGroupNo_JPList"] = dictMap[strAppGroupNo].Substring(1);
}
gv_DataList.DataSource = Tables;
gv_DataList.DataBind();
目前比較常用的手法有這幾種, 就先記錄一下,
讓自己以後回來看到時, 才知道自己在寫什麼~
======
鸞鳳翎毛雨壓垂,
此時應被雀輕欺,
忽朝一日雲霄霽,
依舊還教振羽衣。
======
歡迎轉錄文章,
但記得保留網址和作者名稱~