ASP.NET中DropDownList控件如何绑定数据表中某一列值呀

解决方案 »

  1.   

    数据库列---->DataSet--->DropDownList.DataSource=DataSet
      

  2.   

    OleDbDataAdapter da=new OleDbDataAdapter("select 列名 from 表",conn);
    DataSet ds=new DataSet();
    da.fill(ds,"dfd");
    DropDownList.DataSource=DataSet........
      

  3.   

    SqlDataAdapter myCommand = new SqlDataAdapter("SELECT id,leixing FROM leixing ", myConnection);            myCommand.Fill(ds, "leixing");
                DropDownList2.DataSource = ds.Tables["leixing"].DefaultView;
                DropDownList2.DataTextField = "leixing";
                DropDownList2.DataValueField = "id";
                DropDownList2.DataBind();
      

  4.   

    DropDownList.DataTextField  = "你要显示的字段名";
    DropDownList.DataValueField  = "该字段所在表的ID标识";
    DropDownList.DataBind();
      

  5.   

    同意  happy3613(happy3613) ( )
      

  6.   

    例子:<% @Import Namespace="System.Data" %>
    <% @Import Namespace="System.Data.SqlClient" %>
    <HTML>
    <HEAD>
    <title>DatabindExample9</title>
    <script language=C# runat=server>

    void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
    // 创建数据库连接字符串和SQL语句
    string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
    string query = "SELECT * FROM Products";

    // 执行数据库语句并填充数据到 DataTable 中
    SqlCommand myCommand = new SqlCommand(query, new SqlConnection(ConnStr));
    SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
    DataTable dt = new DataTable();
    myAdapter.Fill(dt); // 数据绑定 DropDownList 控件
    DropDownList1.DataSource = dt;
                                                  //列名
    DropDownList1.DataTextField = "ProductName";
                                                  //对应的列值
    DropDownList1.DataValueField = "UnitPrice";
    DropDownList1.DataBind();
    }
    }

    void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    Label1.Text = "UnitPrice: " + DropDownList1.SelectedValue;
    }

    </script>
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <h3>如何将表中的列绑定到DropDownList控件</h3>
    <br><br>
    请选择:
    <asp:dropdownlist 
    id="DropDownList1" 
    runat="server" 
    AutoPostBack="True" 
    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    </asp:dropdownlist>
    <br><br>
    <asp:label id="Label1" runat="server">UnitPrice:</asp:label>
    </form>
    </body>
    </HTML>
      

  7.   

    就是先操作對數據庫操作。
    然後用SqlDataReader對數據庫進行逐行讀起數據。
    然後保存到
    ArrayList數組中。
    然後將其DropDownList和ArrayList進行綁定即可
      

  8.   

    lohoo水喻舟说得对,按他的方法去做肯定没错。而且是最简单的方法。
    后面的人请不要再回贴了。
    这个问题已经解决了。