all groups > dotnet windows forms databinding > january 2007 >
You're in the

dotnet windows forms databinding

group:

should be really simple databinding queston


should be really simple databinding queston hawbsys
1/30/2007 8:52:48 AM
dotnet windows forms databinding:
Dear all. I'm wondering why it's so hard to bind a lookup value from a
related table into a textbox on my form.

Say I've got two tables, product and productcategory. I'm paging
through product information using the bindingnavigator, one product
per page. Everything on the form is bound to fields in the product
table. Description, price, notes productcategoryid. Nice and simple
Ado.net bound form.

But instead of the ID value for product category I'd like tosee the
productcategory.description. A lookup value. The relationship between
the two tables is defined in the dataset.

All the samples for this kind of thing are parent-child datagrids and
comboboxes. I just want a simple textbox lookup. I don't want
comboboxes. I don't need two way binding, just a readonly lookup.

I can get what I want by a) using a combo, b) using lots of code and
filters on the bindingsource but there must be an easier way. After
all, if it's easy with a combo box (and it is, just slot in the
selectedvalue, displaymember, valuemember), then it's got to be easy-
peasy using a textbox or a label.



hawbsl
Re: should be really simple databinding queston Jim Rand
1/30/2007 1:15:18 PM
Piece of cake:

1) In the product dataset, add the column "Category"
2) In the SELECT statement for the product table, join it with the category
table on productcategoryid. Add the field ProductCategory.Category to the
columns returned. DO NOT change your insert, update and delete statements
as they can only reference one table.
3) To your form, add a text box and bind it to column product.Category.

Jim

[quoted text, click to view]

Re: should be really simple databinding queston hawbsys
1/31/2007 10:57:37 AM
Hi, thanks for answer.

Superficially that would appear to work.

But in fact using JOINS breaks a key element of binding.

If the description for productcategory is changed in another form
(sharing the same dataset) the original form knows nothing about the
change and the "old" name is still shown. The beauty of ADO.NET
databinding, is that you can expect your changes to propagate to all
other parts of your application without extra code. Compare with a
combobox (an updated productcategory description shows seamlessly in
the original form if you change productcategory description in another
form so long as they share the same dataset)

Harry


[quoted text, click to view]

Re: should be really simple databinding queston Jim Rand
1/31/2007 3:30:28 PM
I like that suggestion. Very interesting.

Jim

[quoted text, click to view]

Re: should be really simple databinding queston RobinS
1/31/2007 3:32:04 PM
Please don't multi-post. If you want to post a message in multiple groups,
post all of them at once. That way, if someone answers your question, the
response shows up in all the groups.

Having said that, I posted a response to your almost-identical question in
the microsoft.public.dotnet.framework.adonet group.

And what's wrong with using a ComboBox?

Robin S.
----------------------------------------------------

[quoted text, click to view]

Re: should be really simple databinding queston Bart Mermuys
1/31/2007 7:19:59 PM
Hi,

[quoted text, click to view]

The ComboBox is option 1. What Jim Rand suggests is option 2, though i can
imagine that the TableAdapter wizard would remove the update commands once
you make a sql join, so you would need to restore them manually.

Then there is one other option: use DataRelations and expression columns to
'import' lookup(parent) columns into the master(child).

So in your case you have Product (which is master or child) and Category
(which is lookup or parent), you need to have the _correct_ relation between
those table, then you can add (from code or using the designer) some
columns, eg. :

Product table
- add column:
name = "Description"
expression = "parent(FK_Category_Product).description"

Then you can bind all fields (also the lookups) using a BindingSource for
the Products table.

HTH,
Greetings





[quoted text, click to view]

Re: should be really simple databinding queston hawbsys
2/1/2007 1:45:16 AM
Thanks, that does exactly what I want in terms of behaviour and also
keeps things very simple. Which is perfect.

Harry

[quoted text, click to view]

Re: should be really simple databinding queston hawbsys
2/1/2007 2:03:10 AM
I'm a fan of comboboxes and they bind really nicely. But in UI terms
they suggest to the user that the field is editable and dynamic and I
wanted a plain, uneditable field. A label, really. OK you can set the
combobox's dropdown style to simple and that gets rid of the dropdown
arrow. But the only quick and easy way to stop the user editing a
combobox is to set it to enabled=false. And that greys it out, which
looks rubbish. OK, so you can build your own custom control inheriting
from the combobox which will be readonly but not greyed out and I have
one of those in my project.

So, to get the result I wanted, I was going to have to use a combox,
set all the combobox bindings, remove the dropdown arrow. Only, it
wouldn't be the built in combobox, it would be my own user control
version of the combobox to solve the greying out, and hey presto,
after all that messing about, I would have exactly what I wanted. So
when I posted originally, I was thinking phew ... there has to be a
simpler way.

Thanks to everyone for their answers. Bart's Expression property of a
Column object is the way to go. I hadn't really investigated that
possibility before, but it solves my original problem and probably a
few others too.

Harry

Sorry for the multipost.

[quoted text, click to view]

Re: should be really simple databinding queston RobinS
2/1/2007 9:46:19 AM
Cool. Congrats on finding the solution that works great for you.

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
----------------------------------------------------
[quoted text, click to view]

AddThis Social Bookmark Button