Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

Join Tek-Tips
*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.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Binding LiNQ data to ComboBox (Windows Forms)Helpful Member! 

djfrear (Programmer) (OP)
31 Oct 07 8:19
I am having a few problems working out how to bind LiNQ data to a ComboBox.

Lets say I get my data e.g.

CODE

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?
Helpful Member!  stsuing (Programmer)
31 Oct 07 12:19
Here are some nice samples
http://msdn2.microsoft.com/en-us/vcsharp/aa336746.aspx


http://msdn2.microsoft.com/en-us/vcsharp/aa336748.aspx


//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";
djfrear (Programmer) (OP)
31 Oct 07 13:28
Cheers, apparently I just didn't look hard enough!

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!

Back To Forum

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