Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.


Binding LiNQ data to ComboBox (Windows Forms)

Binding LiNQ data to ComboBox (Windows Forms)

I am having a few problems working out how to bind LiNQ data to a ComboBox.

Lets say I get my data e.g.


var intData = from p in feederData.Pickup_points
                              where p.Interchange == Interchange
                              select p;

How exactly would I go about getting that IQueryable object into the right format in which to bind it to my ComboBox?

I tried casting it to a dataset, but that didnt work too well ;) I imagine there's an easier way than iterating through the object and adding each item to a dataset?

RE: Binding LiNQ data to ComboBox (Windows Forms)

Here are some nice samples

//Here is the example class
    internal class car
            private string _make;
            private string _model;
            private string _id;

            public string Make
                get { return _make; }
                set {_make = value;}
            public string Model
                get { return _model; }
                set { _model = value; }
            public string ID
                get { return _id; }
                set { _id = value; }

//Code to populate and bind to combobox

 List<car> cars = new List<car>();
            cars.Add(new car(){Make = "Ford", Model="Mustang",ID="1"});
            cars.Add(new car() { Make = "Ford", Model = "F150", ID = "2" });
            cars.Add(new car() { Make = "Ford", Model = "Thunderbird", ID = "3" });
            cars.Add(new car() { Make = "Honda", Model = "Accord", ID = "4" });
            cars.Add(new car() { Make = "Honda", Model = "Civic", ID = "5" });
            cars.Add(new car() { Make = "Honda", Model = "Element", ID = "6" });

            var result = from p in cars
                         where p.Make == "Ford"
                         select p;

           this.comboBox1.DataSource = result.ToArray();
            //this works too
            //   this.comboBox1.Items.AddRange(result.ToArray());
             this.comboBox1.DisplayMember = "Model";
            this.comboBox1.ValueMember = "ID";

RE: Binding LiNQ data to ComboBox (Windows Forms)

Cheers, apparently I just didn't look hard enough!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close