|
-
Apr 2nd, 2006, 06:56 PM
#1
Thread Starter
PowerPoster
[2.0] databinding
I am new to databinding in VS.NET 2005.
I remember I used to just select a UI control, then set it's databinding value on the text property and enter the field name to bind to, in VS.NET 2003
I am pretty much trying to do this in VS.NET 2005 but of course it has changed, I know for the better.
I have not set a dataadapter or dataset in the project, it only connects/executes queries or whatever within the code.
What I am wondering is if there is still a way to databind the controls the way it was done in VS.NET 2003?
I do not wish to have to create a connection "set"/dataadapter using the wizard on the databinding property of a UI control.
any ideas?
Thanks
-
Apr 2nd, 2006, 07:11 PM
#2
Re: [2.0] databinding
Keep in mind that while a lot has been added to .NET 2.0 almost nothing has been removed. There are additional data-binding options in 2005 but all the old options still exist. If you used DataBindings.Add in 2003 you can still do exactly the same in 2005.
-
Apr 2nd, 2006, 11:45 PM
#3
Thread Starter
PowerPoster
Re: [2.0] databinding
I apologise I just remembered, and forgot what I was doing
I think i was referring to datalist in ASP.NET - which still exists (I believe, not yet used) in .NET 2.0 and it was/is easy to just bind the controls
but i am referring to WinForms in .NET 2.0 now, how do i databind a collection of textboxes/controls? I know i can use DataReader but that is really for retrieving, say, a couple of fields or a field and populating a combobox for example - ideal for small things
but i want to be effecient and do it the proper way/correct/best practice way for WinForms.
how do i do this?
-
Apr 3rd, 2006, 12:09 AM
#4
Re: [2.0] databinding
It depends what you're binding to the controls. If it's data from a database then your best bet is to populate a DataTable and then bind that to your control(s). The simplest way to populate a DataTable is with a DataAdapter, but if you prefer to use a DataReader then I've made a codebank submission that shows you how (see my sig). It's in VB.NET but it's easily convertible to C#. Once you have a DataTable then it's a signle line of code. Let's say you want to display the values from the Name column in a TextBox:
Code:
myTextBox.DataBindings.Add("Text", myDataTable, "Name");
You may also want to read up about the BindingSource class. It's supposed to simplify the binding process further by providing a generalised link between the actual source and the control. I believe that they make navigation easier but I've not used one explicitly myself so I don't know all the details as yet.
-
Apr 3rd, 2006, 07:08 AM
#5
Thread Starter
PowerPoster
Re: [2.0] databinding
thanks
-
Apr 3rd, 2006, 07:24 AM
#6
Thread Starter
PowerPoster
Re: [2.0] databinding
p.s i would go for a dataadapter approach...how would a bind the datatable to the textboxes upon filling a datatable?
-
Apr 3rd, 2006, 07:37 PM
#7
Thread Starter
PowerPoster
Re: [2.0] databinding
Sorry again, as well as this I would like to use a stored procedure, giving it the parameter I need to give it - how can I do all this in WinForms?
dont worry about the stored proc, i have that and works fine - I am just wanting to know how I can bind the textbox text property to the record retrieved from SQL.
Thanks!
-
Apr 3rd, 2006, 07:42 PM
#8
Re: [2.0] databinding
Note that the retrieval of the data is not specific to WinForms. Create an SqlDataAdapter. Add the required parameter to the SelectCommand and set its CommandType to StoredProcedure and CommandText to the name of the SP. Call the adapter's Fill method to populate a DataTable. Use the code I provided in post #4 to bind the columns of the DataTable to the appropriate controls. When you have edited the data call the Update method of the adapter to execute its UpdateCommand on the DataTable.
-
Apr 3rd, 2006, 07:43 PM
#9
Thread Starter
PowerPoster
Re: [2.0] databinding
thanks, ill give this a bash and get back
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|