Link to home
Start Free TrialLog in
Avatar of techsuppoprt
techsuppoprt

asked on

ComboBox AddRange vs DataSource dinding dilemma

Hi Experts.

I ran into a bit of a dilemma here. Need your advise please.

I have an Array() that's being passed from my Data Layer that I need to bind to the ComboBox.

I need to be able to populate the ComboBox with the array items and add one more item stating "Select User...".

The problem is that if I use cboUsers.DataSource = ...Visual Studio wont allow me to add extra items to the list by doing cboUsers.Items.Add().

and

If I use cboUsers.Items.AddRange() I can't access ValueMembers later on ( not sure why ). It returns Null.

Can anyone advise ?
Thank you.

The code for my Array and ComboBox is below
var userNames = (from u in usersCollection  //IEnumerable collection
                                 select new
                                 {
                                     Id = u.Id,
                                     Name = u.Name
                                 }).ToArray();
                cboUsers.Items.AddRange(userNames);
                cboUsers.DisplayMember = "Name";
                cboUsers.ValueMember = "Id";

Open in new window

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi techsuppoprt;

You need to use the DataSource of the ComboBox control as shown below.

var userNames = (from u in usersCollection  //IEnumerable collection
                                 select new
                                 {
                                     Id = u.Id,
                                     Name = u.Name
                                 }).ToArray();

cboUsers.DataSource = userNames;
cboUsers.DisplayMember = "Name";
cboUsers.ValueMember = "Id";

Fernando
Avatar of techsuppoprt
techsuppoprt

ASKER

Right... as I said.. in that case I have a problem with adding "Select User..." to the combobox.

VIsual Studio does not allow extra stuff to be added to databound objects it seem.
Sorry I was to quick to answer. This is what you need

public class Users
{
    public string Name { get; set; }
    public string id { get; set; }
}


List<Users>  userNames = (from u in usersCollection  //IEnumerable collection
                          select new Users
                          {
                              Id = u.Id,
                              Name = u.Name
                          }).ToList();
           
// Create the item to be inserted into the list
Users selectUser = new Users {Name = "Select User...", id = "0"};
// Insert to list at position 1
userNames.Insert(0, selectUser);

cboUsers.DataSource = userNames;
cboUsers.DisplayMember = "Name";
cboUsers.ValueMember = "Id";
This is a great solution Fernando, thank you but unfortunately it's not going to work in my specific case just because that Users class isn't available for direct access. You might recall you helped me with some stuff earlier :)

I had an Idea..

The statement below returns an array, right ?

var userNames = (from u in usersCollection  //IEnumerable collection
                                select new
                                {
                                    Id = u.Id,
                                    Name = u.Name
                                }).ToArray();

Array means that I should be able to iterate though it...
What if I iterate through this array somehow, throw it into the BindingSource, add my extra "Select One..." and then DataBid it with the DataGridView.

Can you help me put it together ?
//added points
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is truly awesome.
Thank you for not just answering my question but for going into more details and making sure that I understand it all ! Because I do now!
Not a problem, glad I was able to help.  ;=)