询问一下,如何将数据库中取出的值赋给DropDownList
我的代码如下,希望高手赐教
string ds=ConfigurationSettings.AppSettings["System_manage"]; SqlConnection myConnection=new SqlConnection(ds);
SqlCommand SelectCommand;
SelectCommand=new SqlCommand("select * from ProjectDetailedTable where id=@id");
SelectCommand.Connection=myConnection;
SelectCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4));
myConnection.Open();
SelectCommand.Parameters["@id"].Value=Session["uid"].ToString();
SqlDataReader myred=SelectCommand.ExecuteReader();
myred.Read();
txtid.Text=SelectCommand.Parameters["@id"].Value.ToString();
DropDownList1.SelectedValue.ToString()=myred["type"].toString();

解决方案 »

  1.   

    ddlPortCharge.Items[0].Text
    ddlPortCharge.Items[0].Value
    就能赋值
    ListItem li=new ListItem("文本","值");
    ddl.Items.Add(li);
    这样可以添加一个新的
      

  2.   

    是不是查找出数据,然后下拉框选中对应项             ListItem li = DropDownList1.Items.FindByValue(myred["type"].toString());
                if (li != null)
                    li.Selected = true;//选中对应的项
      

  3.   

    恩,就是3楼说,希望在DropDownList1下拉框的选项等与数据库中的值
      

  4.   

    string ds=ConfigurationSettings.AppSettings["System_manage"]; SqlConnection myConnection=new SqlConnection(ds); 
    SqlCommand SelectCommand; 
    SelectCommand=new SqlCommand("select * from ProjectDetailedTable where id=@id"); 
    SelectCommand.Connection=myConnection; 
    SelectCommand.Parameters.Add(new SqlParameter("@id",SqlDbType.Int,4)); 
    myConnection.Open(); 
    SelectCommand.Parameters["@id"].Value=Session["uid"].ToString(); 
    DropDownList1.DataSoure=SelectCommand.ExecuteReader(); 
    DropDownList1.DataTextField="显示值";
    DropDownList1.DataValueField="ID值";
    DropDownList1.DataBind();