(1)   

         string SQL_Select = "select id, ItemName from DDLItem order by id desc";

         //构造一个SqlDataAdapter

         SqlDataAdapter myAdapter = new SqlDataAdapter( SQL_Select, Conn);

         //开始读取数据

         Conn.Open();

         DataSet dataSet = new DataSet();

         myAdapter.Fill( dataSet,"Table1" );

         Conn.Close();

         //开始绑定DropDownList

         //指定DropDownList使用的数据源

         DropDownList1.DataSource = dataSet.Tables["Table1"].DefaultView;

         //指定DropDownList使用的表里的那些字段

         DropDownList1.DataTextField = "ItemName"; //dropdownlist的Text的字段

         DropDownList1.DataValueField = "id";//dropdownlist的Value的字段

         DropDownList1.DataBind();


(2)
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {     
            DropDownList1.Items.Add(new ListItem(dr["status"].ToString(), dr["status_Id"].ToString()));
        }

 

 

插入数据库:

DropDownList1.SelectedItem.Value.ToString()

从数据库读取数据并且设置默认值:

   DropDownList1.Items.FindByText("值").Selected = true;

或者通过value

 DropDownList1.Items.FindByValue(value值).Selected = true; 

 

 

相关文章:

  • 2021-10-26
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
猜你喜欢
  • 2021-08-27
  • 2022-02-23
  • 2022-12-23
  • 2021-07-19
  • 2022-12-23
  • 2021-06-12
相关资源
相似解决方案