I have an instance of employee (from northwind generated with nettiers) that I use throughout a form to create/update/delete etc ...
2 Questions about databinding :
1. Can I bind my form controls to that entity at design time (the employee entity is declared on the form level)
2. For now I do the binding in code (textbox1.Databinding.Add("Text", employee, "Name") which works ok but only until the employee is filled with another row : then I have to clear the bindings and redo them.
It seem to me there's a better solution for this?
Robert Hinojosa ------------------------------------- Member of the Codesmith Tools, .netTiers, teams http://www.nettiers.com-------------------------------------
I looked to the samples, but I can't get it working, as what I want to do is a bit different :
Step 1 :
the follwoing 'products' TList is used as datasource for a combobox in a form. products =
the follwoing 'products' TList is used as datasource for a combobox in a form.
products =
Step 2 :
when the buttons 'modify' or 'create' are clicked : when modify : product = products[listBox1.SelectedIndex] when create : product=new product();
when the buttons 'modify' or 'create' are clicked :
when modify : product = products[listBox1.SelectedIndex]
when create : product=new product();
Step3 :
the following binding works , but I have to run it each time the 'product' entity is filled up (when using the modify or create button, see step 2) textBox1.DataBindings.Add("Text", product, "Descr"); I was hoping to create this binding once (somewhere in the form load) or do I need to redefine it each time 'product' is filled up ? Note : the form holds the comobobox with the products and the product details
the following binding works , but I have to run it each time the 'product' entity is filled up (when using the modify or create button, see step 2)
textBox1.DataBindings.Add("Text", product, "Descr");
I was hoping to create this binding once (somewhere in the form load) or do I need to redefine it each time 'product' is filled up ?
Note : the form holds the comobobox with the products and the product details
Your problem is that in step 3 you are telling textBox1 to bind the "Text" property to the "Descr" property of the instance held in variable product. You are not binding textBox1 to the variable. That is why you need to do it over and over again. You could get fancy and do something like
textBox1.DataBindings.Add("Text", listBox1, "SelectedItem")
and make sure that the newly added or modified product was the one selected in listBox1 (presuming that SelectedItem somehow can get turned into a string for the Text property of textBox1).
I think I get your point.
The solution you suggest though, will only work for modifying a product, not if I also want to add a new one...
Are you using .Net 1.1 or .Net 2.0?
If .Net 2.0 then you could use the BindingSource class and something like the following psuedo-code:
--load bindingSource1 = new System.Windows.Forms.BindingSource(); textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", bindingSource1, "Descr", true));
--step1products = DataRepository.ProductProvider.GetBySupplierID(Convert.ToInt32(txtSearch.Text));bindingSource1.DataSource = products;
--step2if (create){ newProduct = new Product(); bindingSource1.Position = products.Add(newProduct);}
if (modify){ bindingSource1.Location = bindingSource1.Find("ProductID", listBox1.SelectedValue); --or whatever your product id column is}
I hope that is of some use.
Mark.
Mark,
It works !!
I thought I had already tried this solution, without succes, but anyway, I tried again and now it works.
Thanks a lot !
Pascal.