简体   繁体   中英

How to insert item at the top of Combo Box?

Hi I am using Linq to SQl to bind the combo box control. How can i add a item at the top of the list of combo box?

var items = from c in db.Contacts
                               orderby c.Name ascending
                               select c;
                if (items.ToList().Count > 0)
                {
                    cmb1.BindingContext = new BindingContext();
                    cmb1.DataSource = items;
                    cmb1.DisplayMember = "Name";
                    cmb1.ValueMember = "ID";
                }

                cmb1.Items.Insert(0, "--Select--");

This above code is failing.

One way would be to insert the "--Select--" contact place-holder into the results before binding:

     var items = (from c in db.Contacts
                  orderby c.Name ascending
                  select c).ToList();

     items.Insert(0, new Contact { ID = 0, Name = "--Select--" });

     cmb1.BindingContext = new BindingContext();
     cmb1.DataSource = items;
     cmb1.DisplayMember = "Name";
     cmb1.ValueMember = "ID";

Or you could do the same thing with an anonymous version of the results:

     var items = (from c in db.Contacts
                  orderby c.Name ascending
                  select new { c.ID, c.Name }).ToList();

     items.Insert(0, new { ID = 0, Name = "--Select--" });

     cmb1.BindingContext = new BindingContext();
     cmb1.DataSource = items;
     cmb1.DisplayMember = "Name";
     cmb1.ValueMember = "ID";

As you noted in your own comment to your question, you can't insert an item after data binding.

One option would be to use Concat to concatenate a sequence containing your "Select" item with the results of your query.

All the above does not work. I tried all and end up with the below. Linq query output enumerables and lists seems to be immutable like Arrays

I hope your requirement satisfies the below

cbo.Items.Clear();

        Region optAll = new Region { RegionName = "All", RegionId = 0 };

        var qOrg = (from rows in LDC.Regions orderby rows.RegionName 
                    select new
                        {
                            rows.RegionId,
                            rows.RegionName
                        }).ToList();
        if(needAll)
            cbo.Items.Add(optAll);

        foreach (var region in qOrg)
        {
            cbo.Items.Add(region);
        }

        cbo.DisplayMember = "RegionName";
        cbo.ValueMember = "RegionId";

Add a blank selection at the top of list after bound to combo box OnLoad. EF 6

 db.Users.Load(); var updateList = db.Users.Local.ToBindingList(); updateList.Inset(0, new User { ID=0, UserId = "" }); this.userComboBox.DataSource = updateList} 

Insert the --Selected-- item not directly to the combo.items collection but to the items list that you binded to the combo.

List = linq query;
List.Insert(0, "--Select--");

combo.Datasource = List;

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