当前位置: 代码迷 >> ASP.NET >> dropDownList绑定数据有关问题
  详细解决方案

dropDownList绑定数据有关问题

热度:2500   发布时间:2013-02-25 00:00:00.0
dropDownList绑定数据问题
我想在此控件上加一个 "---请选择--- "
我的绑定代码是这样写的:
SqlConnection   con   =   new   SqlConnection( "server=.;database=jqwdb;uid=sa ");
                string   sql   =   "select   *   from   goodsType ";
                SqlDataAdapter   da   =   new   SqlDataAdapter(sql,   con);
                DataSet   ds   =   new   DataSet();
                da.Fill(ds);
                this.DropDownList1.DataSource   =   ds.Tables[0].DefaultView;
                this.DropDownList1.DataTextField   =   "goodsType ";
                this.DropDownList1.DataValueField   =   "goodsId ";
                this.DropDownList1.DataBind();

------解决方案--------------------------------------------------------
this.DropDownList1.DataBind();
加上一行

this.DropDownList1.Items.Insert(0, new ListItem( "---请选择--- ", "top "));
------解决方案--------------------------------------------------------
同上
------解决方案--------------------------------------------------------
加上
this.DropDownList1.Items.add(new ListItem( "---请选择--- ", "0 "));
this.DropDownList1.SelectedIndex=this.DropDownList1.Items.Count-1;

------解决方案--------------------------------------------------------
lv = new ListItem();
lv.Text = "---请选择--- ";
lv.Value = "0 ";
DropDownList1.Items.Add(lv);
if (DropDownList1.Items.Count > 0)
{
DropDownList1or.SelectedIndex = 0;
}
------解决方案--------------------------------------------------------
SqlConnection con = new SqlConnection( "server=.;database=jqwdb;uid=sa ");
string sql = "select * from goodsType ";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds);
this.DropDownList1.DataSource = ds.Tables[0].DefaultView;
this.DropDownList1.DataTextField = "goodsType ";
this.DropDownList1.DataValueField = "goodsId ";
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0, new ListItem( "---请选择--- ", "-1 "));
------解决方案--------------------------------------------------------
啊哦
------解决方案--------------------------------------------------------
DropDownList1.Items.Insert(0, new ListItem( "---请选择--- ", "-1 "));
------解决方案--------------------------------------------------------
DropDownList1.Items.Insert(0, new ListItem( "---请选择--- ", "-1 "));
------解决方案--------------------------------------------------------
如果是VS2005,直接点智能标记,编辑项
------解决方案--------------------------------------------------------
vs2005智能标记,编辑项 AppendDataBoundItems= "True "
------解决方案--------------------------------------------------------
DropDownList1.Items.Insert(0, new ListItem( "---请选择--- ", "-1 "));
加“-1”是什么意思?
----------------
指定这一项的Value,根据实际情况,此值不要与其它绑定项的Value值相同
  相关解决方案