WPF LinqToSql ComboBox Master Detail

B

Bill McCormick

Hello,

I'm trying to find an example and/or advice on how to setup the bindings
on basic WPF controls like ComboBox and TextBox that will get their data
from LinqToSql. I'd like to do something like use a ComboBox as a record
selector and display/edit in TextBox'es other data from the selected
master record as well as a detail from a linked record in another table.

I'm using the book "Programming WPF - 2nd Edition" but I'm not getting
everything.



Thanks,

Bill
 
C

Cor Ligthert[MVP]

Bill,

You can try this one,

It is with the NorthWind SQL sample database
\\\
DataClasses1DataContext db = new DataClasses1DataContext();
var customers = from customerRows in db.Customers
select customerRows;
comboBox1.ItemsSource = customers;
comboBox1.DisplayMemberPath = "ContactName";
comboBox1.SelectedValuePath = "CustomerID";
///

Cor
 
C

Cor Ligthert[MVP]

Bill,

You can try this one,

It is with the NorthWind SQL sample database
\\\
DataClasses1DataContext db = new DataClasses1DataContext();
var customers = from customerRows in db.Customers
select customerRows;
comboBox1.ItemsSource = customers;
comboBox1.DisplayMemberPath = "ContactName";
comboBox1.SelectedValuePath = "CustomerID";
///

Cor
 
B

Bill McCormick

Mr. Arnold said:
Well, you're going to use link-sql-query to only bringing back an ID and
Description off the Entity for the table that's going to load the combox.

Then I suspect that you're going to need a class Bill.cs that has the
get/set properties for ID and Description files.

Then you're going to go into a foreach loop, creating a new Bill each
time in the loop populating Bill and adding Bill to a List<Bill>.

That would be var bills = new List<Bill>(); that you would load Bill
into the collection of bills.

Then you'll take bills and bind it to the control.datasource.

I'll assume you know how to use the ComboBox Selection event and get the
selected line's ID.

Then you take the selected ID to do another link-2-sql query to select
the record by ID.

var hit = (from a in table where a.ID == ID select a).First();

hit.Name if name is a field on the table can be selected.

Textbox1.Text = hit.Name

That's how I would do it.

You completely lost me after you said that I'm "going to need a class
Bill.cs ...".

You must have missed that I'm using LinqToSql? Or maybe I'm somehow
using it differently than you?

When using LinqToSql, it will auto-magically generate a whole set of
classes that mirror the schema of the backend data. The IDE does that
when you drag-n-drop tables onto the designer.

It creates a class that inherits from System.Data.Linq.DataContext and
each time you add a new table, it will generate code for a new property
to return a generic list of table rows defined by a class that it also
generates code for. So, the classes are all there and I should NOT need
to create a "Bill" class to populate the combo.

Can you provide some examples of how to code the XAML? Particularly the
attributes "DataContaxt", "ItemsSource", "DisplayMemberPath",
"SelectedItem", etc.

Also, I'm creating a UserControl that will have a grid that will contain
all the UI widgets. How would I code the XAML for <ObjectDataProvider>
with the <UserControl.Resources> so that all controls on the grid have
the same DataContext?


Thanks,


Bill
 
B

Bill McCormick

Mr. Arnold said:
Well, you're going to use link-sql-query to only bringing back an ID and
Description off the Entity for the table that's going to load the combox.

Then I suspect that you're going to need a class Bill.cs that has the
get/set properties for ID and Description files.

Then you're going to go into a foreach loop, creating a new Bill each
time in the loop populating Bill and adding Bill to a List<Bill>.

That would be var bills = new List<Bill>(); that you would load Bill
into the collection of bills.

Then you'll take bills and bind it to the control.datasource.

I'll assume you know how to use the ComboBox Selection event and get the
selected line's ID.

Then you take the selected ID to do another link-2-sql query to select
the record by ID.

var hit = (from a in table where a.ID == ID select a).First();

hit.Name if name is a field on the table can be selected.

Textbox1.Text = hit.Name

That's how I would do it.

You completely lost me after you said that I'm "going to need a class
Bill.cs ...".

You must have missed that I'm using LinqToSql? Or maybe I'm somehow
using it differently than you?

When using LinqToSql, it will auto-magically generate a whole set of
classes that mirror the schema of the backend data. The IDE does that
when you drag-n-drop tables onto the designer.

It creates a class that inherits from System.Data.Linq.DataContext and
each time you add a new table, it will generate code for a new property
to return a generic list of table rows defined by a class that it also
generates code for. So, the classes are all there and I should NOT need
to create a "Bill" class to populate the combo.

Can you provide some examples of how to code the XAML? Particularly the
attributes "DataContaxt", "ItemsSource", "DisplayMemberPath",
"SelectedItem", etc.

Also, I'm creating a UserControl that will have a grid that will contain
all the UI widgets. How would I code the XAML for <ObjectDataProvider>
with the <UserControl.Resources> so that all controls on the grid have
the same DataContext?


Thanks,


Bill
 
B

Bill McCormick

Cor said:
Bill,

You can try this one,

It is with the NorthWind SQL sample database
\\\
DataClasses1DataContext db = new DataClasses1DataContext();
var customers = from customerRows in db.Customers
select customerRows;
comboBox1.ItemsSource = customers;
comboBox1.DisplayMemberPath = "ContactName";
comboBox1.SelectedValuePath = "CustomerID";
///
OK. I think I got that far already. But my question now is how to get
other data fields from the selected customers record to display in other
controls like a TextBox? And then, how to code so that a linked orders
record will display in yet other TextBox's?


Thanks,

Bill
 
B

Bill McCormick

Cor said:
Bill,

You can try this one,

It is with the NorthWind SQL sample database
\\\
DataClasses1DataContext db = new DataClasses1DataContext();
var customers = from customerRows in db.Customers
select customerRows;
comboBox1.ItemsSource = customers;
comboBox1.DisplayMemberPath = "ContactName";
comboBox1.SelectedValuePath = "CustomerID";
///
OK. I think I got that far already. But my question now is how to get
other data fields from the selected customers record to display in other
controls like a TextBox? And then, how to code so that a linked orders
record will display in yet other TextBox's?


Thanks,

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top