询问一下,DropDownList1能否同时绑定一个表中两列的值

解决方案 »

  1.   

    可以
    Text一列
    Value一列
      

  2.   

    一般Text是给用户看的,Value是给开发人员使用的,经常绑定的是主键
      

  3.   

    当然,要在用户显示的text上绑定两条就不行了
      

  4.   

    我的意思是我一个表中的字段有两个分别是name,id
    我希望在DropDownList1的下拉框中显示name+id
    System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection(); 
    sqlconn.ConnectionString ="user id=sa;password=;initial catalog=System_manage;Server=.";
    sqlconn.Open(); 
    //System.Data.SqlClient.SqlDataAdapter sqldar = new System.Data.SqlClient.SqlDataAdapter("select table1.id,table2.name from LoginTable table1 join LoginTable table2 on table1.phone=table2.phone",sqlconn); 
    System.Data.SqlClient.SqlDataAdapter sqldar = new System.Data.SqlClient.SqlDataAdapter("select id,name from LoginTable",sqlconn); 
    sqldar.SelectCommand.CommandType = CommandType.Text;
    System.Data.DataSet DataSet1= new System.Data.DataSet(); 
    sqldar.Fill(DataSet1,"Users");
    DropDownList1.DataSource = DataSet1.Tables["Users"].Columns.ToString(); 
    DropDownList1.DataTextField="id";
    DropDownList1.DataTextField="name";
    DropDownList1.DataValueField="name";
    DropDownList1.DataBind(); 
    Response.Write("绑定成功");
    sqlconn.Close();  
    下面是部分代码,希望各位看一下
      

  5.   

    问题解决了大家看一下吧
    System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection(); 
    sqlconn.ConnectionString ="user id=sa;password=;initial catalog=System_manage;Server=.";
    sqlconn.Open(); 
    System.Data.SqlClient.SqlDataAdapter sqldar = new System.Data.SqlClient.SqlDataAdapter("select cast(id as varchar(10)) + a1 as name1 from ac",sqlconn); 
    sqldar.SelectCommand.CommandType = CommandType.Text;
    System.Data.DataSet DataSet1= new System.Data.DataSet(); 
    sqldar.Fill(DataSet1,"Users");
    DropDownList1.DataSource = DataSet1.Tables["Users"].DefaultView; 
    DropDownList1.DataTextField="name";
    DropDownList1.DataValueField="name"
    DropDownList1.DataBind(); 
    sqlconn.Close()