Databinding to an object

MondeoST24

Member
Joined
Mar 9, 2006
Messages
16
Programming Experience
Beginner
I have a simple database app, tab 1 has a datagrid on which is filled simply with SELECT * FROM Customers into a dataset/datatable which is then bound to the grid.Then if I double click a row it moves to tab 2, looks up cell 1 on the selected row of the datagrid (which is the primary key) and then does "SELECT * FROM Customers WHERE ID=id" into a datareader.On tab 2 I have loads of text boxes and I simply dotxtname.text = rdr.item(0).tostringtxtcompany. text = rdr.item(1).tostringAnd so on.Could someone explain the correct and accepted way of doing this task, i.e. using object databinding etc. Should I create a customer class first with properties for my database fields? Then how to I do the binding etc.Thanks
 
The correct way of doing this task.. uhm, if you have a million customers, is not to laod all the data into the client.. Just a subset of it.. say, all the J Smiths. We will leave that to later though because right now the bigger problem is your bindings should be arranged differently. I'll draw a picture:
 

Attachments

  • untitled.GIF
    untitled.GIF
    2.9 KB · Views: 35
Last edited:
The datatable is loaded with data. The bindingsource looks at that data. All the controls are attached to the binding source.

The bindingsource only manipulates one line of data at once - the current item. It is capable of showing all the other lines.

The datagridview shows all lines in the binding source. The current item is whichever line in the datagridview is the focused (not selected) one.

The textboxes only show the current line. The ultimate way this works is that the data is filled ONCE. The position is changed in the datagridview, and the text boxes automatically change which line of data they are looking at. no requery necessary.


Read the DW2 link in my signature. Follow the instructions of Fetching Data Into Your Application. On a single form, put:

One Datagridview, One Textbox. Have them bound to the same bindingsource, that is bound to the data. The IDE will generate all code for you (except maybe the SQL). I have attached a sample application, simply because it was faster for me to create it than to type the advice :)
 

Attachments

  • WindowsApplication1.zip
    7.8 KB · Views: 24
Last edited:
Back
Top