Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ComboBox AddRange vs DataSource dinding dilemma

Posted on 2008-11-03
7
Medium Priority
?
2,680 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:techsuppoprt
  • 4
  • 3
7 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22869278
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
0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 22869309
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.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22869509
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";
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:techsuppoprt
ID: 22872091
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
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 22873876
Hi techsuppoprt;

To your questio:

    The statement below returns an array, right ?
    Answer: It returns an Array of Anonymous type.

    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.
    Answer: Because you are dealing with Anonymous type of an Array in the function
            or event that you run the above query in this is possible with some
            overhead. Anonymous types are immutable like strings so if you add an
            item to the collection, in the above case userNames, you will be creating
            a completly new collection with the added item leaving Garbage Collection
            to clean up the old Array. From outside the function or event that the
            query was created in which it used the Anonymous type you would need to
            use .Net Reflections Classes to be able to get access to the data.
            Which means that you cannot directly access the values as can be with the
            solution I posted.
           
You state that, "unfortunately it's not going to work in my specific case just because
that Users class isn't available for direct access." If you look at my code below I
show that the Users class can be accessed directly.

Fernando

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        // List of Users is directly accessable 
        List<Users> userNames;
 
        public class Users
        {
            public string Name { get; set; }
            public string id { get; set; }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            DbCDataContext db = new DbCDataContext();
 
            userNames = (from c in db.Customers
                         select new Users { Name = c.ContactName, id = c.CustomerID }).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.Add(selectUser);
 
            comboBox1.DataSource = userNames;
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "id";
            
            // Or if you want to use a data binding abd a data grid view then this 
            // will also work
            bindingSource1.DataSource = userNames;
            dataGridView1.DataSource = bindingSource1;
        }
 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox1.SelectedIndex;
            // Access the values of the Users class with the use of the comboBox1 control
            Console.WriteLine("User name = {0}\tID = {1}", comboBox1.Text, comboBox1.SelectedValue);
 
            // Access the Users class directly using the selected index of the comboBox1
            Console.WriteLine("User name = {0}\tID = {1}", userNames[index].Name, userNames[index].id);
        }
    }
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:techsuppoprt
ID: 31512787
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!
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 22882697
Not a problem, glad I was able to help.  ;=)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question