Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binding LiNQ data to ComboBox (Windows Forms) 1

Status
Not open for further replies.

djfrear

Programmer
Jul 11, 2007
83
0
0
GB
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?
 
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";
 
Cheers, apparently I just didn't look hard enough!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top