Results 1 to 9 of 9

Thread: [2.0] databinding

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    [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

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: [2.0] databinding

    thanks

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    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!

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: [2.0] databinding

    thanks, ill give this a bash and get back

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width