Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 19th, 2008, 04:33 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Winforms

I am writing a Winforms app using Sql Server 2005. I want to do databinding by letting the page load, then fetch data whenever an event happens. My 'navigation controls' are represented by one databound comboBox bound to a sorted dataView. Now on the SelectionChange event I want to do a DB fetch and populate some radio buttons and checkboxes, but I can't get it working. How do you actually do this in Winforms?

Any help appreciated.
Thx


Reply With Quote
  #2 (permalink)  
Old May 19th, 2008, 04:36 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

I think the easiest way to implement this sort of behaviour is to create a DataSet based on the required data and bind that to the Form. When using generated DataSets you also get your type safety and full intellisense support, note that if the data changes following generation, then the code needs to be re-generated.

DataSets will manage all the control population as well as updates back to the data for you. You can use DataReaders to pull the data from the DB, but you then need to manage concurrency yourself which can be a pain in the ass, especially since the DataSet can handle it all for you out of the box.

Have a google on DataSets and DataBinding in Winforms, theres tons of info on how to implement this. If you get stuck then feel free to shout :)

Rob
http://robzyc.spaces.live.com
Reply With Quote
  #3 (permalink)  
Old May 19th, 2008, 04:44 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thx. I have googled and browsed books on this for days. I managed to get the comboBox behaving as I need. I create a dataView in VS2005, pick the display field, value field and sort column. It works like I want.

What you said implies having two bits of databinding, one to get the ComboBox functioning, and another to manage what data the form displays, and the comboBox is used to position the forms 'current record'. Can this be done?
I can find no examples quite matching this in any book or webpage.
Thx.

Reply With Quote
  #4 (permalink)  
Old May 19th, 2008, 04:59 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Yes, basically on a form each container control (Form, any panels) has a BindingContext collection, which exposes a CurrencyManager object which in turn manages a DataBinding (updates, positioning etc.) within that container control (there can be many).

Binding a forms controls to a dataset will enable you to populate and update values based on changes, for most simple controls this is simply a case of binding the "Text" property to the data source, for more complex items such as ComboBox you would bind the ValueMember property to the main DataSet and set the DisplayMember to the related record.

A good overview and walkthrough can be found here: http://www.akadia.com/services/dotnet_databinding.html

I am suprised you have not found examples of this common routine, nearly every development book I have ever read has contained a section on DataBinding..

I hope this helps!

EDIT: w00t 300th post!

Rob
http://robzyc.spaces.live.com
Reply With Quote
  #5 (permalink)  
Old May 19th, 2008, 05:43 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thx, it does help. I'm getting there but I realise why I was having so much trouble. I might have a custom problem:

I have a column that can take any one of three values: Yes(A), Yes(B), and NO.

I have three controls on the form representing this field: A Checkbox which if unchecked means NO, or if its checked then two RadioButtons in a group become visible, Yes(A) and Yes(B).

I use a combobox bound to a sorted dataview as the navigational control for the forms 'current record'. The underlying record should be updated when the user clicks the update button.

The form is drawn and the combobox is finished (apart from the SelectedIndexChanged event code). There is no navigation wired up though.

I don't know how to databind the Checkbox/Radiobuttons + Group, is the prob. I'm sure it can be done.
Reply With Quote
  #6 (permalink)  
Old May 19th, 2008, 05:47 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

In this case, you want to check the data when the record is changed by tying it up to the PositionChanged event (I believe), query the current datas value and then re-organise your display as necessary (hiding/showing controls etc).

As for DataBinding the Option group, try here: http://tinyurl.com/5kov9f

Rob
http://robzyc.spaces.live.com
Reply With Quote
  #7 (permalink)  
Old May 19th, 2008, 08:06 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've got it. Databind an invisible textbox to the field, set the controls from this textbox on form Load and update the textbox in control event processing. Way.
Reply With Quote
  #8 (permalink)  
Old May 19th, 2008, 08:12 AM
Friend of Wrox
 
Join Date: Mar 2007
Location: Hampshire, United Kingdom.
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

What?! No! You shouldnt need to make anything invisible?! Just create a DataSet based on the data you require, bind the controls relevant properties on the form to the required data in the DataSet. If you need to "fancy" logic to alter the display based on the data, then query the dataset directly in your codebase.

Clues in the title "DataBinding". The elements (Text, whatever) and Objects (DataSet) are BOUND to the data, in essence, when bound, the Text IS the data.

Rob
http://robzyc.spaces.live.com
Reply With Quote
  #9 (permalink)  
Old May 20th, 2008, 03:52 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'll tell you what, if I get it working I will post the code then if it can be done more appropriately I'll be glad to adopt it.
Reply With Quote
  #10 (permalink)  
Old May 20th, 2008, 05:59 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I see what you mean.
I bound my textboxes to the same datasource as the combobox. When I selected a different entry in the combobox the textboxes displayed the new record's data without my having to add any navigation code.
They even retained the changes I made when I navigated off and back again. It's not updating back through to SQL Server though - I think I have a little more to learn.
I'm also not sure I can do without the invisible textboxes yet.
Ta.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in using Browser in Winforms jay69 C# 0 December 5th, 2006 04:49 AM
Posting Data to a Website using Winforms c# jay69 C# 1 December 2nd, 2006 05:33 AM
Debugging Winforms gaghazoul Visual Studio 2005 0 May 18th, 2006 01:43 PM
Deleting rows in Winforms Datagrid livehed General .NET 0 May 4th, 2006 01:23 AM
autocomplete combo in vb.net winforms imsuneeta General .NET 1 January 25th, 2005 06:00 AM



All times are GMT -4. The time now is 10:46 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.