首页->文章内容
C#如何将数据绑定到comboBox上
[编辑:pinglan] [点击:1756] [回应:0] [上传时间:2018-9-12 21:52:26]

C#如何将数据绑定到comboBox上

如果是ASP.NET,将数据绑定到DropDownList上,可以用SqlDataReader这样绑定:

        SqlCommand mymand = new SqlCommand(txt, Con);

        SqlDataReader dr = mymand.ExecuteReader();

        this. aBase.DataSource = dr;

        this. aBase.DataTextField = "name";

        this. aBase.DataBind();

如果是C#,没有DropDownList控件,可以用comboBox,将属性DropDownStyle设置成DropDownList,然后:

            SqlDataAdapter Adapter = new SqlDataAdapter(txt, Con);

            DataSet da = new DataSet();

            Adapter.Fill(da);

            comboBox3.DataSource = da.Tables[0];

            this.comboBox3.DisplayMember = "mingzi";

可见,2 个控件用了不同的方法,一个是单程读取,加一个是数据适配器。




乐 排 行 板 . 版 权 所 有

京ICP备12001047号-3