标签:
using System; using System.Collections.Generic; using System.Data; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ProjectWeb { public partial class DataSetTable : System.Web.UI.Page { //创建字典,好处就是使用ContainsKey功能 private Dictionary<string, string> dicObject = new Dictionary<string, string>(); protected void Page_Load(object sender, EventArgs e) { //把DataTable数据添加到Dictionary字典 DataTable dt_sn = GetData().Tables[0]; this.dicObject.Clear(); foreach (DataRow dr in dt_sn.Rows) this.dicObject.Add(dr["id"].ToString(), dr["name"].ToString()); //Dictionary字典的数据添加到页面DropDownList1 this.DropDownList1.Items.Clear(); this.DropDownList1.Items.Add(new ListItem("第一张表"," ")); foreach (KeyValuePair<string, string> kv in dicObject) this.DropDownList1.Items.Add(new ListItem(kv.Value, kv.Key)); //第二张表数据 DataTable dt_te = GetData().Tables[1]; this.DropDownList2.Items.Clear(); this.DropDownList2.Items.Add(new ListItem("第二张表", " ")); foreach (DataRow dr in dt_te.Rows) this.DropDownList2.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString())); } #region 返回数据集 private DataSet GetData() { //创建第一张表 DataTable dt_sn = new DataTable(); //添加列 dt_sn.Columns.Add("id"); dt_sn.Columns.Add("name"); //添加行 DataRow dr_sn = dt_sn.NewRow(); dr_sn["id"] = "1"; dr_sn["name"] = "杨秀徐"; dt_sn.Rows.Add(dr_sn); //表名称 dt_sn.TableName = "SN"; //创建第二张表 DataTable dt_te = new DataTable(); //添加列 dt_te.Columns.Add("id"); dt_te.Columns.Add("name"); //添加行 DataRow dr_te = dt_te.NewRow(); dr_te["id"] = "2"; dr_te["name"] = "杨四节"; dt_te.Rows.Add(dr_te); //表名称 dt_te.TableName = "Te"; DataSet ds = new DataSet(); //把表DataTable添加到数据集DataSet ds.Tables.Add(dt_sn); ds.Tables.Add(dt_te); return ds; } #endregion } }
标签:
原文地址:http://www.cnblogs.com/sntetwt/p/4702600.html