开发者

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.


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;


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}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜