日期:2014-05-19  浏览次数:20472 次

100分----求下拉列表框读数据代码
表:t1
-----------
ID       name     country
1         jim       中国
2         tom       中国
3         bom       英国

需要一个下拉列表框来显示country的值.
这里就应该显示这个表里面所有的country的值了.
如果我选一行.然后点button.
搜索出T1里面的被选country的数据.
并且这个下拉列表框的默认值是所有.如果不选的话.就认为是所有.搜索出来的就应该是所有的数据.

请教各位专家.这里应该怎么写啊.可以给个例子吗?


------解决方案--------------------
把数据绑定到里面可以撒,另外加个“请选择”项
你在BUTTON事件中写个条件,如果dropdownlist的索引为请选择就循环读取所有用foreach
否则就读取所选的索引值。不知道你看明白没

------解决方案--------------------
//默认显示
void ViewData(DataTable dt)
{
DataView dv = new DataView(dt);
//或者DataView dv=dt.DataView();
this.DataList1.DataSource = dv;
this.DataList1.DataBind();
}

//字符长度筛选控制显示
void ViewData_Country(DataTable dt)
{
DataView dv = new DataView();
dv.Table = dt;
dv.RowFilter = "country = " + ddl.SelectedItem.Value;
this.DataList1.DataSource = dv;
this.DataList1.DataBind();
}


------解决方案--------------------
现将t1表中country列绑定到DropDownList1 别忘了distinct一下
绑定后应如下
=====================================
page_load里
DropDownList1.Items.Add(new ListItem( "中国 ", "中国 "));
DropDownList1.Items.Add(new ListItem( "英国 ", "英国 "));
DropDownList1.Items.Insert(0,new ListItem( " ", "默认全部 "));//绑定后自己加的一默认项

==================================
button里的事件

string str = " ";
if(DropDownList1.SelectedValue == "默认全部 ")
{
for(int itemC=1 ;itemC <DropDownList1.Items.Count;itemC++)
{
str += DropDownList1.Items[itemC].ToString().Trim() + ", ";
}
str = str.Substring(0,str.Length-1);
}
else
{
str = DropDownList1.SelectedValue;
}

string sqlStr = string.Format( "select * from t1 where country in({0}) ",str);
//上面的是得到的查询的sql 用它来进行查询操作吧

------解决方案--------------------
con.Open();
SqlCommand cmd=new SqlCommand( "select * from chengshi ",con);
SqlDataReader sdr=cmd.ExecuteReader();
this.DropDownList1.DataSource=sdr;
this.DropDownList1.DataTextField= "chengshimc ";
this.DropDownList1.DataValueField= "chengshiid ";
this.DropDownList1.DataBind();
sdr.Close();
cmd.CommandText= "select * from xianqu where chengshiid=1 ";
sdr=cmd.ExecuteReader();
this.DropDownList2.DataSource=sdr;
this.DropDownList2.DataTextField= "xianqumc ";
this.DropDownList2.DataValueField= "xianquid ";
this.DropDownList2.DataBind();
sdr.Close();
con.Close();
this.DropDownList1.Items.Insert(0,new ListItem( "请选择城市 ", "0 "));
this.DropDownList2.Items.Insert(0,new ListItem( "请选择县区 ", "0 "));
// this.DropDownList1.Items[0].Selected=true;
this.DropDownList1.SelectedIndex=0;
this.DropDownList2.SelectedIndex=0;