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
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] "hawbsys" <Harry.Andrews@wbsys.co.uk> wrote in message news:1170175968.623204.19690@v45g2000cwv.googlegroups.com... > 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 >
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] On 30 Jan, 18:15, "Jim Rand" <jimr...@ix.netcom.com> wrote: > 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 > > "hawbsys" <Harry.Andr...@wbsys.co.uk> wrote in message > > news:1170175968.623204.19690@v45g2000cwv.googlegroups.com... > > > > > 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- Hide quoted text - > > - Show quoted text -
I like that suggestion. Very interesting. Jim [quoted text, click to view] "Bart Mermuys" <bmermuys.nospam@hotmail.com> wrote in message news:zZ5wh.319668$SV1.6256736@phobos.telenet-ops.be... > Hi, > > "hawbsys" <Harry.Andrews@wbsys.co.uk> wrote in message > news:1170175968.623204.19690@v45g2000cwv.googlegroups.com... >> 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. >> > > 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 > > > > > >> >> >> hawbsl >> > >
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] "hawbsys" <Harry.Andrews@wbsys.co.uk> wrote in message news:1170175968.623204.19690@v45g2000cwv.googlegroups.com... > 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 >
Hi, [quoted text, click to view] "hawbsys" <Harry.Andrews@wbsys.co.uk> wrote in message news:1170175968.623204.19690@v45g2000cwv.googlegroups.com... > 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. >
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] > > > hawbsl >
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] On 31 Jan, 19:19, "Bart Mermuys" <bmermuys.nos...@hotmail.com> wrote: > 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 > > > > > > > hawbsl- Hide quoted text - > > - Show quoted text -- Hide quoted text - > > - Show quoted text -
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] On 31 Jan, 23:32, "RobinS" <Rob...@NoSpam.yah.none> wrote: > And what's wrong with using a ComboBox?
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] "hawbsys" <Harry.Andrews@wbsys.co.uk> wrote in message news:1170324189.951784.260350@q2g2000cwa.googlegroups.com... > 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. > > On 31 Jan, 23:32, "RobinS" <Rob...@NoSpam.yah.none> wrote: > >> And what's wrong with using a ComboBox? > >
Don't see what you're looking for? Try a search.
|