LIST繫結至DropDownList,避免重覆項目的方法
//宣告一個類別
public class Item
{
private string _Text;
private string _Value;
public string Text
{
get
{
return _Text;
}
set
{
_Text = value;
}
}
public string Value
{
get
{
return _Value;
}
set
{
_Value = value;
}
}
}
DataTable dtTmp = SQL.ExecuteQuery("select * from tables ").Tables[0];
List<Item> li = new List<Item>();
foreach (DataRow dr in dtTmp.Rows)
{
Item itm = new Item();
itm.Text = dr["Col1"].ToString();
itm.Value = dr["Col2"].ToString();
//判斷是否有重覆的選單
if (li.Where(Obj => Obj.Text ==itm.Text && Obj.Value ==itm.Value).Count()==0)
{
li.Add(itm);
}
}
DropDownList ddlItem =new DropDownList ();
ddlItem.DataSource = li;
ddlItem.DataTextField = "Text";
ddlItem.DataValueField = "Value";
ddlItem.DataBind();