查询语句写在数据库的一个存储过程中,查询出一个表字段,大概有几十条记录。
如何将这些存储过程查询出的记录绑定在DropDownList控件上,小弟愚蠢,请各位高手指点,谢谢。

解决方案 »

  1.   

    直接把查询出来的结果绑定在DropDownList上就可以了啊
      

  2.   

    DropDownList1.DataSource=读取出来的数据源;
    DropDownList1.DataBind();------------ http://www.voddh.com QVOD免费电影网---------------
      

  3.   

    参考这些,看看是怎样做绑定的:http://www.cnblogs.com/insus/articles/1431940.html
    http://www.cnblogs.com/insus/articles/1411016.html
      

  4.   

    DropDownList1.Items.Add(table1);
    你试一下看看行不行,我没测试过
      

  5.   

    DropDownList1.DataSource=读取出来的数据源;
    DropDownList1.DataTextField= "表里对应要显示的text字段名"; 
    DropDownList1.DataTextValue= "表里对应要显示的value字段名"; 
    DropDownList1.DataBind(); 
      

  6.   


    HTML code
    <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" DataKeyField="au_id">
        <Columns>
            <asp:BoundColumn DataField="au_id" HeaderText="au_id" />
            <asp:TemplateColumn HeaderText="state">
                <ItemTemplate>
                    <asp:DropDownList id="DropDownList1" runat="server" DataSource='<%# GetStates() %>' DataTextField="state" Text='<%# Da…
      

  7.   

    private void BindGrid()
    {
        SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs");
        SqlDataAdapter da = new SqlDataAdapter("select au_id, state from authors", cn);
        DataSet ds = new DataSet();
        cn.Open();
        da.Fill(ds);
        cn.Close();
        DataGrid1.DataSource = ds;
        DataGrid1.DataKeyField = "au_id";
        DataGrid1.DataBind();
    }protected DataTable GetStates()
    {
        SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs");
        SqlDataAdapter da = new SqlDataAdapter("select distinct state from authors", cn);
        DataSet ds = new DataSet();
        cn.Open();
        da.Fill(ds);
        cn.Close();
        return ds.Tables[0];
    }protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        DropDownList dropTemp;
        string strState;
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            strState = ((DataRowView)e.Item.DataItem).Row["state"].ToString();        //对DropDownList做数据绑定
            dropTemp = e.Item.FindControl("DropDownList2") as DropDownList;
            if (dropTemp != null)
            {
                SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=pubs");
                string strSql = "select distinct state from authors";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                cn.Open();
                dropTemp.DataSource = cmd.ExecuteReader();
                dropTemp.DataTextField = "state";
                dropTemp.DataBind();
                cn.Close();            //到DropDownList中根据type的值去找需要设置为选中状态的项目,将其设置为选中
                ListItem item = dropTemp.Items.FindByText(strState);
                if (item != null)
                {
                    item.Selected = true;
                }
            }
        }
    }
      

  8.   

            this.DropDownList1.DataSource = ds.Tables[0].DefaultView;
            this.DropDownList1.DataValueField = "name2";
            this.DropDownList1.DataTextField = "name2";
            this.DropDownList1.DataBind();
      

  9.   

         string sql ="select id,name from table";
            this.DropDownList1.DataSource = ds.Tables[0].DefaultView; 
            this.DropDownList1.DataValueField = "id"; 
            this.DropDownList1.DataTextField = "name"; 
            this.DropDownList1.DataBind();我就是这样用的,结果是对的
      

  10.   

    if(!Ispostback)
    {
    sqlconnection con=new sqlconnection("....");
    string str="select * from tb_friend";
    sqldatadapter ada=new sqldataadapter(str,con);
    dataset ds=new dataset();
    ada.fill(ds,"tb_friend");
    dropdownlist1.datasource=ds;
    dropdownlist1.datavaluefield="friendemail";//绑定某个字段
    dropdownlist1.databind();
    con.close();
    }
      

  11.   

    点下dropdownlist会有一个小图标,点下消图标会出现一个选择数据源,可以再这里对dropdownlist进行绑定,代码是自动生成的,里边有对你要绑定的数据有高级用法,可以自己写SQL语句想要得到什么都可以,
      

  12.   

    必须要用代码的么?打开dropdownlist 右上角的智能图标 选择数据源 新建 sql数据库 默认名称就行
    然后 选择连接字符串 然后 你慢慢看看 吧。呵呵。
      

  13.   

    同意楼上各位的假如查出的表结构是
    ID City
    1  北京
    2  上海
    3  深圳则绑定时一般为:
    this.DropDownList1.DataSource = 数据源;
    this.DropDownList1.DataValueField = "ID"; 
    this.DropDownList1.DataTextField = "City"; 
    this.DropDownList1.DataBind();
      

  14.   

    DropDownList1.DataSource=读取出来的数据源; 
    DropDownList1.DataTextField= "表里对应要显示的字段名"; 
    DropDownList1.DataTextValue= "表里对应要显示的字段名"; 
    DropDownList1.DataBind(); 
    只要数据源没问题,这样写是完全可以的