简体   繁体   中英

How to Bind List<> to ComboBox?

I have this code :

    public static List<string> MyTable = new List<string>();

dsView = new DataSet();

adp = new SqlCeDataAdapter("SELECT DISTINCT Fname FROM MEN", Conn);

adp.Fill(dsView, "MEN");

adp.Dispose();

foreach (DataRow R in dsView.Tables["MEN"].Rows)

GG.Add( R["Fname"].ToString());

how to Bind it to ComboBox ?

thank's in advance

You just set the ComboBox's DataSource equal to the List.

comboBox1.DataSource = MyTable;

If you use a System.ComponentModel.BindingList instead of a List then changes to the list will be sent to the ComboBox

Also should your last line:

GG.Add( R["Fname"].ToString());

be

MyTable.Add( R["Fname"].ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM