CodeSmith Community
Your Code. Your Way. Faster!

Databinding to a nettiers entity

rated by 0 users
This post has 6 Replies | 2 Followers

Top 150 Contributor
Posts 36
Points 1,025
pascald Posted: 04-28-2006 8:31 AM

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?

 

 

  • | Post Points: 35
Top 10 Contributor
Posts 2,797
Points 254,125
Yes, you can do it at design time.  You can either check out this lab.
http://community.codesmithtools.com/forums/thread/9954.aspx

Or the winforms project in this thread.
http://community.codesmithtools.com/forums/thread/9597.aspx


Robert Hinojosa
-------------------------------------
Member of the Codesmith Tools, .netTiers, teams
http://www.nettiers.com
-------------------------------------

  • | Post Points: 35
Top 150 Contributor
Posts 36
Points 1,025

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 = DataRepository.ProductProvider.GetBySupplierID(Convert.ToInt32(txtSearch.Text));

Step 2 :

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

         
  • | Post Points: 35
Top 50 Contributor
Posts 106
Points 2,406

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).

Best regards, Magnus Werner
  • | Post Points: 35
Top 150 Contributor
Posts 36
Points 1,025

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...

  • | Post Points: 35
Top 100 Contributor
Posts 47
Points 1,015

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));

--step1
products = DataRepository.ProductProvider.GetBySupplierID(Convert.ToInt32(txtSearch.Text));
bindingSource1.DataSource = products;

--step2
if (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.

  • | Post Points: 35
Top 150 Contributor
Posts 36
Points 1,025

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.

  • | Post Points: 5
Page 1 of 1 (7 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems