# Friday, November 10, 2006

DevConnections Vegas Slides and Demos

Another great conference complete. Around 5000 showed up and we had great feedback from the crowd that it was a good show. If you haven't been to connections before, you really should check it out.

I gave three talks this week. You can get the slides and demos for each below.

Real World ClickOnce:  Slides   Demos
Workflow Driven Windows Applications:  Slides   Demos
Implement a Data Layer with the VS 2005 DataSet Designer:  Slides   Demos



.NET | .NET 3.0 | Architecture | ClickOnce | Data Binding | DevConnections | Speaking

Friday, November 10, 2006 2:02:33 AM (GMT Standard Time, UTC+00:00)
Comments [0]  | 


 # Tuesday, August 15, 2006

Get it while it is cheap - Data Binding with Windows Forms 2.0 promotion

Bookpool.com is doing a special discount promotion of my Data Binding with Windows Forms book:

http://www.bookpool.com/ct/214

If you haven't picked up a copy yet, here is a chance to save some bucks off the usual price.



.NET | Data Binding

Tuesday, August 15, 2006 5:24:27 PM (GMT Daylight Time, UTC+01:00)
Comments [0]  | 


 # Friday, July 7, 2006

Slides and demos from MSDN Webcast: Build Smart Client Data Applications with Windows Forms 2.0

For those who attended, or those who just want the materials, here are the slides and demos from today's MSDN Webcast:

Slides: http://www.softinsight.com/downloads/MSDNWebcasts/SmartClientDataApplications_Jul06.pdf

Demos:http://www.softinsight.com/downloads/MSDNWebcasts/SmartClientDataAppsDemos_Jul06.zip

 



.NET | Community | Data Binding | Speaking

Friday, July 7, 2006 6:31:57 PM (GMT Daylight Time, UTC+01:00)
Comments [0]  | 


 # Monday, June 19, 2006

Another TechEd Complete - Slides and Demos

It was a crazy week at TechEd last week. So crazy, no time to write or consume blogs. I gave two breakout sessions (Real World ClickOnce and Windows Forms: Build Enterprise Ready Forms Applications) and a Birds of Feather session (Windows Workflow Foundation).

You can get the slides and demos from the sessions here:

Real World ClickOnce:  Slides   Demos

Windows Forms: Build Enterprise Ready Forms Applications:  Slides   Demos

 



.NET | ClickOnce | Data Binding | Languages and Tools | Speaking

Monday, June 19, 2006 2:27:37 PM (GMT Daylight Time, UTC+01:00)
Comments [0]  | 


 # Saturday, May 20, 2006

Slides and Demos from SDC Netherlands

I gave four talks at the Software Developers Conference in Netherlands this week. This is a very fun and interesting conference that is put on by a large user group organization called Software Developers Network, run by Remi Caron and Joop Pecht.

This conference is one of the most enjoyable conferences I get to do anywhere in the world. It is amazing how professional and well run this conference is, especially when you consider that it is being put on by a user group organization and it is better run than many U.S. conferences put on by companies that are supposed to specialize in this kind of event. All of the user group members that run the conference are volunteers, and yet the quality and professionalism that comes out of that is outstanding.

The attendees are hard core, ask great questions, and make the event fun for the speakers as well. For those of you who attended and find your way to this post for the slides and demos - thanks!

You can grab the slides and demos here:

Build Smart Client Data Apps with Windows Forms 2.0:  Slides   Demos
Build Custom Data Bound Objects and Collections:  Slides   Demos
Present Rich Tabular Data with the DataGridView Control:  Slides   Demos
Drive Application Behavior with Application and User Settings:  Slides   Demos



.NET | ClickOnce | Community | Data Binding | Languages and Tools | Speaking | Travel

Saturday, May 20, 2006 2:35:03 PM (GMT Daylight Time, UTC+01:00)
Comments [0]  | 


 # Saturday, May 13, 2006

DevTeach Slides and Demos

I spoke at DevTeach  in Montreal Tue-Thu of this week and had a great time as always. If you haven't checked out this conference, you should plan on signing up next year. Great location, great speakers, very well done conference with lots of hard core sessions.

If you attended one of my sessions and want to get the slides and demos, here you go:

NET371 - Drive App Behavior with Application and User Settings:  Slides   Demos

NET391 - Custom Bound Objects and Collections:  Slides   Demos

NET463 - Advanced ClickOnce:  Slides   Demos

MusicLibrary Database Creation Script:   Script



.NET | ClickOnce | Community | Data Binding | Languages and Tools | Speaking | Travel

Saturday, May 13, 2006 6:09:02 PM (GMT Daylight Time, UTC+01:00)
Comments [0]  | 


 # Monday, March 13, 2006

Data Binding Talk in San Diego - Slides and Demos

I gave a talk on Data Binding with Windows Forms 2.0 at the San Diego .NET Developers Group on Tuesday 7 Mar.

Here are the slides and demos:  Slides   Demos



.NET | Community | Data Binding | Languages and Tools | Speaking

Monday, March 13, 2006 6:30:14 AM (GMT Standard Time, UTC+00:00)
Comments [0]  | 


 # Monday, February 27, 2006

What to do when BindingNavigator Raises Exception on AddNew

I got a great question from a reader recently. It's essence reads like this:

If I set up drag and drop data binding to a table that has non-nullable columns, and then press the Add New button twice in the BindingNavigator, I get an unhandled exception on the thread. Since all of the code involved in that call chain is in .NET code and assemblies, how can I handle the exception to keep it from blowing up my app?

If you are not already familiar, to get to this point, you have to create a data bound UI using the Data Sources window, or by hooking up the controls manually. What you end up with after dragging a collection from the Data Sources window onto a form is:

  • A DataGridView or Details form of individual controls
  • A BindingSource component that is set as the data source of the grid or the individual controls
  • A BindingNavigator control that is hooked up to the BindingSource component.

If your data source is a typed data set in the same project, you also get a table adapter instance and data set instance as members on the form, and a Form.Load event handler that fills the appropriate table of the data set so that the app functions without any hand written code. If your data source is coming from a different assembly (an Object data source), then it will be up to you to go retrieve an instance of the collection type and set it as the DataSource property on the BindingSource at runtime to complete the data binding chain.

The way the BindingNavigator gets hooked up, it just points to the BindingSource component and uses the API exposed by a BindingSource to navigate forward and back and to add and delete items from the underlying collection. When you press the Add New button on the BindingNavigator, it calls the AddNew method on BindingSource. The BindingSource passes the call to the underlying collection if it implements the IBindingList interface. Calling AddNew usually also implicitly calls EndEdit on the current item if that item type implements the IEditableObject interface, depending on the collection type's implementation of the AddNew method.

So when dealing with a data table as your collection, you are actually bound to its default DataView. The DataView class implements the IBindingList interface, and the DataRowView class (the items in the collection) implement IEditableObject. When a column in the table is set up so that it does not accept null values, the DataRowView implementation of EndEdit will throw and exception when EndEdit is called if the non-nullable columns have not been provided a value.

The call chain that sets all this up for a standard data set based application is that the BindingNavigator calls into the BindingSource and calls AddNew. This calls into the DataView and adds a new row to the table and starts an editing transaction by calling BeginEdit on the row. When you press the AddNew button a second time, EndEdit is called on the first row you added, which, if you haven't filled in the non-nullable columns, will throw an exception. Since the call chain goes from BindingNavigator to BindingSource to DataView to DataRowView, there is no user code in the call chain where you can logically insert an exception handler.

You could handle the situation in a crude form by having an Application.ThreadException handler, which will catch all unhandled exceptions on the thread. However, this doesn't get called until the stack has unraveled all the way back out to the base of the call stack, so it is a little late to be dealing with the exception in a recoverable way.

A better solution is to inherit from the BindingSource component and provide your own implementation to AddNew. The following implementation (thanks to Steve Lasker and Daniel Herling on the product team in Redmond for coming up with this) shows how:

private class MyBindingSource : BindingSource

{

   public MyBindingSource()

      : base()

   {

   }

 

   public override object AddNew()

   {

      object o = null;

      try

      {

          o = base.AddNew();

      }

      catch (System.Exception ex)

      {

          this.OnDataError(

             new BindingManagerDataErrorEventArgs(ex));

      }

      return o;

   }

}

With this in place, you can just handle the DataError event on the BindingSource component to do whatever is appropriate based on the exception.



.NET | Data Binding | Languages and Tools

Monday, February 27, 2006 6:29:04 PM (GMT Standard Time, UTC+00:00)
Comments [0]  | 


 # Friday, February 24, 2006

.NET Rocks and DNRtv episodes up

I recorded a DNR and DNRtv last week in New London and they are already up on the site.

You can download/listen to the .NET Rocks! epsidode here: http://www.dotnetrocks.com

And the DNRtv here: http://www.dnrtv.com

In the DNR episode, we talk about data binding, ClickOnce and a few other related topics.

This DNRtv shows how to do some of the data binding stuff in the designer. Keep your eyes out for another episode in a week or so on ClickOnce deployment.



.NET | ClickOnce | Community | Data Binding | Languages and Tools | Speaking

Friday, February 24, 2006 2:45:20 PM (GMT Standard Time, UTC+00:00)
Comments [0]  | 


 # Wednesday, February 22, 2006

Debugging SQL Express Apps - Beware VS Copy To Output Directory default

Here is a little trick that has bitten me on more than one occasion, and just bit someone who attended my data binding session in NYC the other night.

Here is the setup to be bitten by the defaults in Visual Studio:

- Add a SQL Express database to your project

- Edit the schema, add some tables, etc.

- Write some code / data binding that uses the database

- Run a debug session adding records or modifying ones you created through the designer.

- Save the changes from your running debug session.

- Shut down the app and run another debug session, and your changes are no longer there.

First instinct at this point is "there is something screwed up with my data binding / data access code". That instinct may be wrong (although we are all good at writing bugs as well).

When VS adds the MDF file to your project, it places it and its related LDF file in the project root folder, and add them to your project in solution explorer. The file properties for the MDF file include Build Action = Content, and Copy To Output Directory = Copy Always.

What is happening is that your changes are being persisted to the copy of the MDF file that was placed in your build output directory (bin\debug) on the first debug run. Then on the second debug run, the unchanged MDF file from the project root folder is copied down into the build output folder, overwriting the one that was there with your changes from the previous run. As a result, it looks like your changes were not persisted when in fact they were (or may have been if your code was correct).

The solution is that whenever you add a SQL Express DB to your VS project, you probably want to change the Copy To Output Directory property for the file to Copy If Newer.



.NET | Data Binding | Languages and Tools

Wednesday, February 22, 2006 1:21:46 PM (GMT Standard Time, UTC+00:00)
Comments [0]  | 


 # Monday, February 20, 2006

.NET Rocks! and .NET Rocks! TV Episodes coming up
I went up to New London this week and taped two episodes of DNRtv and one DNR with Carl and Richard. The DNRtv episodes should go up in the next two weeks, one on data binding and one on ClickOnce deployment. The DNR will air on 22 March. Check them out!

.NET | ClickOnce | Community | Data Binding | Languages and Tools | Speaking

Monday, February 20, 2006 6:10:56 PM (GMT Standard Time, UTC+00:00)
Comments [0]  | 

Data Binding with Windows Forms 2.0 Slides/Demos from NYC.NET

I gave a talk on data binding in NYC this Thu night. Had a great time. Lively crowd as always, lots of good questions and interaction.

Here are the slides and demos:

Slides    Demos



.NET | Community | Data Binding | Languages and Tools | Speaking

Monday, February 20, 2006 6:06:58 PM (GMT Standard Time, UTC+00:00)
Comments [0]  | 


 # Thursday, January 19, 2006

Data Binding with Windows Forms 2.0 Table of Contents

A couple people have suggested that I post the table of contents for my book to my blog since it is not yet available on Amazon.

Here it is:

Foreword xxi

Preface xxiii

Acknowledgments xxxv

About the Author xxxvii

Chapter 1: Building Data-Bound Applications with Windows Forms 1

What Is Data Binding? 2

Your First Data-Bound Windows Forms 2.0 Application 3

Data-Binding Landscape 14

Data Sources 15

Data Objects and Collections 16

DataSets or Not, That Is the Question... 18

Data-Bound Controls 20

Layered Application Architecture 21

What Is a Smart Client? 27

Where Are We? 28

Chapter 2: Working with Typed Data Sets and Table Adapters 31

A Quick Review of DataSets 31

The Quest for Type Safety 34

Typed Data Set Internals 37

Creating Typed Data Sets 41

Creating Typed Data Sets with the Data Set Designer 42

Typed Data Set-Generated Code 49

Introduction to Table Adapters 52

Filling and Updating a Typed Data Set with a Table Adapter 56

Connection Management 58

Adding Transaction Support to a Table Adapter 62

Adding Helper Data Access Methods 66

Basing Table Adapters on Stored Procedures or Views 67

Adding Queries to Table Adapters 69

Creating Typed Data Sets with Command Line Tools 77

Using Typed Data Sets in Your Code 78

Where Are We? 79

Chapter 3: Introducing Data Binding in Windows Forms 81

The 40,000-Foot View of Data Binding 81

Binding Data Collections to a Grid 86

Binding Data Collections to Multi-Valued Controls 88

Binding Data to Individual Controls on a Form 90

Data Paths Within Data Sources 92

Synchronizing Data Between Controls 96

Smarter Data Containment 97

Paging Through Data 99

Master-Details Data Binding 104

Updating Data Sources Through Data Binding 106

Where Are We? 108

Chapter 4: Binding Controls to Data Sources 111

Getting to Know the BindingSource Component 111

Simple Data Binding with Binding Sources 112

Chaining Binding Sources for Master-Details Data Binding 116

Navigating Data Through a Binding Source 121

Manipulating Data Through a Binding Source 122

Using a Binding Source as a Data Storage Container 124

Filling a Binding Source with a Data Reader 126

Sorting, Searching, and Filtering Presented Data with a Binding Source 128

Monitoring the Data with Events 131

Restricting Changes to the Data 133

Underneath the Covers of Data Binding for Complex Types 134

Binding an Image Column to a PictureBox Control 141

Binding a DateTime Column to a DateTimePicker 142

Binding a DateTime Column to a TextBox 144

Binding a Numeric Column to a TextBox 145

Automatic Formatting and Parsing Summary 147

Going Beyond Built-In Type Conversion with Binding Events 148

Handling the Format Event 154

Handling the Parse Event 156

Completing the Editing Process 157

Making the User’s Life Easier with AutoComplete 160

Data Binding Lifecycle 162

Smarter Child-Parent Data Binding 163

Binding to Multiple Copies of Data 165

Updating Parent Data-Bound Controls from Child Data-Bound Controls 168

Synchronizing Many-to-Many Related Collections 172

Where Are We? 176

Chapter 5: Generating Bound Controls with the Visual Studio Designer 177

Working with the Data Sources Window 177

Adding Data Sources to a Project 179

Choosing the Type of Data Source 180

Adding a Database Data Source 181

Adding a Web Service Data Source 185

Adding an Object Data Source 186

Generating Bound Controls from Data Sources 189

Selecting the Bound Control Type 196

Customizing the Bound Control Types 196

Binding Existing Controls to Data Sources 199

Behind the Scenes: Designer Code and Data Sources Files 202

Other Designer Data-Binding Code Generation 205

Setting Control Data Binding Through the Properties Window 206

Generating Data Bindings with Smart Tags 210

Generating Master-Details Data-Bound Controls with the Designer 214

Where Are We? 216

Chapter 6: Presenting Data with the DataGridView Control 217

DataGridView Overview 218

Basic Data Binding with the DataGridView 219

Controlling Modifications to Data in the Grid 221

Programmatic DataGridView Construction 222

Custom Column Content with Unbound Columns 226

Displaying Computed Data in Virtual Mode 233

Using the Built-In Column Types 241

Built-In Header Cells 255

Handling Grid Data Edits 256

Automatic Column Sizing 259

Column and Row Freezing 262

Using the Designer to Define Grids 263

Column Reordering 266

Defining Custom Column and Cell Types 269

Utilizing Cell-Oriented Grid Features 277

Formatting with Styles 281

Where Are We? 284

Chapter 7: Understanding Data-Binding Interfaces 285

What Does Data Binding Have to Do with Interfaces? 286

The IEnumerable and IEnumerator Interfaces: Supporting Iteration Through Collections 289

The ICollection Interface: Controlling Access to a Collection 295

The IList Interface: Enabling Data Binding 298

The IListSource Interface: Exposing Collections of Collections 303

Property Descriptors: Allowing Dynamic Data Item Information Discovery 305

The ITypedList Interface: Exposing Data-Binding Properties 307

The IBindingList Interface: Providing Rich Binding Support 310

The IBindingListView Interface: Supporting Advanced Sorting and Filtering 323

The ICancelAddNew Interface: Supporting Transactional Inserts in a Collection 325

The IRaiseItemChangedEvents Interface: Providing Item Modification Notifications on Collections 327

The IEditableObject Interface: Supporting Transactional Item Modifications 328

The INotifyPropertyChanged Interface: Publishing Item Change Notifications 329

The ICustomTypeDescriptor Interface: Exposing Custom Type Information 332

The ISupportInitialize Interface: Supporting Designer Initialization 334

The IDataErrorInfo Interface: Providing Error Information 330

The ISupportInitializeNotification Interface: Supporting Interdependent Component Initialization 337

The ICurrencyManagerProvider Interface: Exposing a Data Container’s CurrencyManager 341

Where Are We? 341

Chapter 8: Implementing Custom Data-Bound Controls 343

Extending Framework Data-Bound Controls 344

Creating a Grouped Column DataGridView 345

Using Custom Controls 350

The User Control Test Container 352

Developing Data-Bound Container Controls 353

Building a Filtered Grid Control 354

Adding Data-Binding Capability to a Custom Control 357

Supporting Designer Initialization of Data Binding 359

Specifying Binding Properties on a Control 360

Supporting Delayed Initialization with ISupportInitialize 362

Dynamically Determining the Properties of a Data Source 367

Autocompleting Input in a TextBox Control 371

Autosizing Columns in the Grid 375

Winding Up the Filtered Grid Example 376

Building a Custom Data-Bound Control from Scratch 379

Building a Data-Bound Charting Control for Decision Support 379

Coding a Data-Bound Custom Control 384

Adding Editing Support to a Custom Data Bound Control 391

Where Are We? 397

Chapter 9: Implementing Custom Data-Bound Business Objects and Collections 399

Defining and Working with Data-Bound Business Objects 400

Defining and Working with Data-Bound Business Object Collections 405

.NET Framework Generic Collection Classes 406

The CustomBusinessObjects Example 408

Setting the Textual Data-Binding Behavior of Custom Objects 415

Supporting Transacted Object Editing with IEditableObject 416

Supporting Object Edit Notifications with Property Change Events 420

Supporting Object Edit Notifications with INotifyPropertyChanged 423

Using BindingList<T> to Create Rich Object Collections 424

Creating a Custom Collection Type Based on BindingList<T> 426

Managing Transacted Additions to a Collection 439

Raising Item Changed Events 441

Adding IBindingListView Functionality 443

Binding to Business Objects Through the Data Sources Window 453

Where Are We? 455

Chapter 10: Validating Data Input and Handling Errors 457

Windows Forms Validation 458

Handling Validation Events 459

DataGridView Validation Events 462

Validation Up the Control Hierarchy 463

Displaying Validation Errors with the ErrorProvider Control 464

DataGridView Error Displays 467

DataGridView DataError Event 468

Controlling Validation Behavior with the AutoValidate Property 471

Validation down the Control Hierarchy 472

Extended Validation Controls 474

Capturing Data Errors on Data Sets 475

Providing Error Information from Custom Objects with IDataErrorInfo 479

Data Concurrency Resolution 483

Where Are We? 484

Appendix A: Binding to Data in ASP.NET 487

ASP.NET Page Processing Basics 489

Data Binding in ASP.NET 1.X 490

Data-Binding Overview in ASP.NET 2.0 498

Data Sources 499

Data-Binding Expressions 508

GridView Control 509

DetailsView Control 512

FormView Control 514

Master-Details Binding 515

Hierarchical Binding 518

Where Are We? 519

Appendix B: Binding Data in WinFx Applications 521

WinFx UI Programming and Capabilities Overview 522

Writing a Simple WinFx Application 525

WinFx Data Binding 101 532

Data Contexts and Data Sources 536

What About XAML? 537

Binding a Collection to a Grid with Templates 541

Control Styling in WinFx 543

Where Are We? 545

Appendix C: Programming Windows Forms Applications 547

Your First Windows Forms Data Application 548

Creating Windows Forms Applications with Visual Studio 554

Windows Forms Designer-Generated Code (New in 2.0) 563

A Brief Tour of the Windows Forms Architecture 567

The Dawn of .NET Execution—The Main Method 570

Handling Control Events 574

Displaying Other Forms 576

Containing Forms Within a Parent Form 577

Common Data Display Controls 578

Creating a Custom User Control 586

Laying Out Controls on a Form 589

Setting Tab Order 596

Command and Control of Your Windows Forms Applications (New in 2.0) 598

Where Are We? 600

Appendix D: Accessing Data with ADO.NET 601

Relational Data Access 603

The Ubiquitous DataSet 607

Loading Data Sets from a File 609

Creating a Data Set Programmatically 611

Loading Data Sets from a Database 613

Loading a DataTable with a DataReader 619

Master-Details DataSets 621

Retrieving Data with Stored Procedures 623

Updating the Database Using Data Sets 624

Handling Concurrency 628

Updating with Data Sets and Stored Procedures 632

Searching Data Sets 637

Merging Data from Multiple Data Sets 639

Working with Data Views 641

Working with Transactions 643

Scoping Transactions with System.Transactions 647

Client-Side Transactions 650

Data Set and Data Adapter Events 651

Reading Data into Business Objects 654

XML Data Access 658

Working with the XmlDataDocument Class 659

Working with the XPathDocument Class 663

Loading Data into an XPathDocument 664

Querying XML Data 665

Navigating an XML Document 667

Where Are We? 670

Index 671



.NET | Data Binding | Languages and Tools | Publishing

Thursday, January 19, 2006 1:58:00 PM (GMT Standard Time, UTC+00:00)
Comments [0]  | 


 # Wednesday, January 18, 2006

Data Binding with Windows Forms 2.0 Talk in Sarasota last night

I gave a talk on data binding at the Sarasota FL .NEt Users Group last night. Had a great time, and it was especially fun to present this topic this time since it was the first time presenting on data binding since my book came out. Gave away a couple copies. It was also great to go have some beers with the group members afterwards, including fellow MVPs Stan Schultes and David Hayden.

You can grab the slides and demos here:  Slides   Demos



.NET | Community | Data Binding | Languages and Tools | Speaking

Wednesday, January 18, 2006 2:16:15 PM (GMT Standard Time, UTC+00:00)
Comments [0]  | 

Data Binding with Windows Forms 2.0 is out!

It was a very cool feeling to have a box of my books delivered to me on Friday. After starting way too early on it and rewriting many of the chapters multiple times as the capabilities evolved in Visual Studio 2005 and .NET 2.0, it felt very good to finish the writing a couple months ago. But having the finished product show up on my doorstep was very cool.

So stop reading this and go buy one dammit! :)



.NET | Data Binding | Languages and Tools | Publishing

Wednesday, January 18, 2006 2:10:01 PM (GMT Standard Time, UTC+00:00)
Comments [0]  | 


 # Thursday, January 5, 2006

Data Binding with Windows Forms 2.0 Sample Code Posted

I posted all the sample code for my book Data Binding with Windows Forms 2.0 on the book Web site at http://www.softinsight.com/databindingbook. The samples are available in both C# and VB, even though all the code in the book is in C#. The book should be hitting the shelves very soon and is already selling well on Amazon.

I also posted instructions for running the samples if you do not have a non-default instance of SQL Server or don't have Northwind on your machine yet, also how to run the samples with SQL Server 2005 Express, Visual C# 2005 Express and Visual Basic 2005 Express.

Happy Data Binding!



.NET | Data Binding | Languages and Tools | Publishing

Thursday, January 5, 2006 3:06:29 PM (GMT Standard Time, UTC+00:00)
Comments [0]  | 


 # Thursday, December 1, 2005

Adding a Drop-Down List (ComboBox) Column of Lookup Values in a DataGridView

A common requirement and question that is very easy to solve with the DataGrid is the need to have a column or columns in a grid that contain a drop down list of lookup values from a related table. For example, in Northwind, we have the Products table with a foreign key column SupplierID that relates the to Suppliers table. The values in the Suppliers table could be treated as a list of lookup values for editing the supplier associated with a product:

Application image

When a supplier is selected from the drop down list, it should set the SupplierID column value for that row in the products table. This is very easy to achieve through the Windows Forms designer and the smart tag for a DataGridView control.

Do the following to try it out:

1. Create a Windows Forms Project.
2. Add a data source to a data set that contains Products and Suppliers tables from the Northwind database. This could be any kind of Data Source (Database, Object, Web service), but for simplicity for trying out the designer, just use a database one which adds the typed data set into the Windows Forms project. Products is the table of data we will display in the grid, and Suppliers is the lookup table related through a foreign key column in the Products table that we want to display as a combo box of selectable values in the grid.
3. Drag and drop Products from the Data Sources window onto the form. This will generate the grid, a data set instance, a table adapter to fill it, a BindingSource component hooked up to the data set and the Products table within it, a grid hooked up to the products binding source, and a binding navigator hooked up to the binding source.
4. Click on the grid smart tag (little triangle on upper right border of control).
5. Select Dock in Parent Container.
6. Select Edit Columns.
7. Select the SupplierID column in the list on the left.
8. Select the ColumnType property and set it to DataGridViewComboBoxColumn.
9. Select the DataSource property and navigate down through the data source tree through Other Data Sources > Project Data Sources > NorthwindDataSet > Suppliers table.

data sources property editor
10. Select the DisplayMember property and select CompanyName.
11. Select the ValueMember property and select SupplierID.
12. Click OK to close the Edit Columns dialog.
13. Run the app and observe that you get the drop down list of suppliers. Selecting a supplier actually changes the SupplierID column value for that row in the products table to the appropriate foreign key value.

You can download a completed sample that was generated with these steps here.



.NET | Data Binding | Languages and Tools

Thursday, December 1, 2005 3:05:10 PM (GMT Standard Time, UTC+00:00)
Comments [0]  | 


 # Tuesday, November 29, 2005

DataGridView Webcast demos

Here are the demos from my MSDN Webcast today on the DataGridView control.

You can find the sample DataGridView chapter for my book here.

You can view the webcast on demand through the links here.



.NET | Community | Data Binding | Languages and Tools | Speaking

Tuesday, November 29, 2005 7:44:11 PM (GMT Standard Time, UTC+00:00)
Comments [0]  | 


 # Wednesday, November 9, 2005

Build Custom Data Bound Business Objects and Collections Talk at DevConnections this morning

I just got finished doing my first talk here at DevConnections in Vegas and I think it went pretty well. Great crowd, good questions, fun topic.

You can grab the slides and demos here: Slides   Demos

The talk highlighted how to define custom objects and collections to make them suitable for data binding, mostly for Windows Forms, but some of it is applicable to ASP.NET as well.

The key takeaways from the talk are:

  • Implement INotifyPropertyChanged on any business entitity type you define that you expect to use in data binding scenarios. This interface defines a contract for the objects to raise PropertyChanged events whenever a property is set on the object. It allows containing collections or bound controls to be notified when the contents of the data object change, which helps with keeping controls synchronized in a form.
  • Use BindingList<T> to create strongly typed collections of objects that support rich data binding. It provides full implementation of IList, ICollection, IEnumerable and their generic strongly typed counterparts for whatever type parameter you provide, and it provides a partial implementation of the IBindingList interface. The part that it implements is firing ListChanged events when items are added or removed from the collection. It also looks at the objects type that you provide as a type parameter, and if it implements INotifyPropertyChanged, the collection will subscribe to the PropertyChanged event on each object and raise ListChanged events with a change type of PropertyChanged whenever the contents of an object in the collection change. These features make BindingList<T> collections work seamlessly with data binding to multiple controls and keeps the controls all in sync.
  • Use my BindingListView<T> class (in the demos and in my book) to get a generic container that supports sorting (both IBindingList based single property sorts and IBindingListView multi-property sorts), searching, and filtering.

If you were there at the end and saw the on-the-fly demo where I didn't see the saving behavior that I thought I had just implemented, I tracked down the problem. the changes were actually being saved. It was just the way I hooked up the data binding I wasn't seeing those changes.

In the demo, I used the data sources window to generate a Details view (control collection) bound to a collection of Album data. I changed one of the controls in the collection which was bound to a GenreID property on the Album objects to a ComboBox. I then used the Data Sources window to add data binidng to a Genre object collection to populate the list of Genres in the combo box. This sets up the ComboBox to have its contents determined by the Genre collection, but its SelectedValue property is bound to the GenreID property on the current item in the Album collection - generally exactly what you want to be able to edit a property on one object collection item through a lookup list of values in another collection of objects. The problem was that after I selected a new value in the combo box and saved the changes, then restarted the app, I wasn't seeing the modified value set for the Album I was viewing in the other controls.

It turns out the problem was just the order that I did the initial binding of the control collection and the combobox BindingSources. In the form load I had added the following two lines of code to bind the control collection and the combo box binding sources:

albumBindingSource.DataSource = Album.GetAlbums();
genreBindingSource.DataSource = Genre.GetGenres();

The problem is that when you set the DataSource, that is when it intializes the bound controls. So I was initializing the data bindings for all of the controls based on the album data, then intializing the combo box of Genres with a new set of data. That set the SelectedIndex of the ComboBox back to zero, so I wasn't seeing the actual value of the Genre for the current Album, I was just seeing the first Genre value in the Genre collection. The fix is simply to do the initial binding in the reverse order:

genreBindingSource.DataSource = Genre.GetGenres();
albumBindingSource.DataSource = Album.GetAlbums();

Then it works as expected.

Some resources I mentioned in the talk, as well as some additional ones I gave related to after-session questions:

My Book: Data Binding in Windows Forms 2.0, Addison Wesley, January 2006
http://www.amazon.com/exec/obidos/ASIN/032126892X/qid%3D1124482085/sr%3D11-1/ref%3Dsr%5F11%5F1/102-3039504-6850510
Rocky Lhotka binding refresh problem post:
http://www.lhotka.net/WeBlog/PermaLink.aspx?guid=d8306469-7e76-4734-9811-777498808b85
Rocky Lhotka article on binding to business objects: Windows Forms Object Data Binding in .NET 2.0, 15seconds.com, http://www.15seconds.com/issue/040614.htm
My recent article on The Server Side .NET: Build a Data Access Layer with the Visual Studio DataSet Designer, The Server Side .NET, Oct 2005, http://www.theserverside.net/articles/showarticle.tss?id=DataSetDesigner
My recent article in CoDe magazine: Tackle Complex Data Binding in Windows Forms 2.0, CoDe Magazine, July/Aug 2005, http://www.code-magazine.com/Article.aspx?quickid=0507051

Enjoy!



.NET | Data Binding | DevConnections | Languages and Tools | Speaking

Wednesday, November 9, 2005 8:26:45 PM (GMT Standard Time, UTC+00:00)
Comments [0]  | 


 # Monday, October 24, 2005

Two Upcoming MSDN Webcasts: Part of the "Best Of" Series

I've got two MSDN Webcasts coming up at the end of November, both part of the "Best Of" series that they are doing surrounding the launch of VS 2005 for those Webcasts focused on .NET 2.0 and VS 2005 that got the highest scores in the last year.

You can click through here to get to the webcasts:

Click Through for Webcasts

The two I am giving will both be on 29 November:

Presenting Rich Rich Tabular Data with the DataGridView Control
Tuesday, November 29, 2005
10:00 A.M.–11:00 A.M. Pacific Time

Deploy Smart Client Applications with ClickOnce
Tuesday, November 29, 2005
1:00 P.M.–2:00 P.M. Pacific Time

Check them out!!



.NET | ClickOnce | Community | Data Binding | Languages and Tools | Speaking

Monday, October 24, 2005 6:59:35 PM (GMT Daylight Time, UTC+01:00)
Comments [0]  | 


 # Wednesday, October 12, 2005

Building Smart Client Data Applications with Windows Forms 2.0

I gave a talk at LexDotNet in Lexington KY last night on building smart client data apps. It really boils down to a similar talk to my Tackle Complex Data Binding with Windows Forms 2.0 talk, but with a slightly different approach and angle. I did mostly a large progressive demo where I started out with the simple and impressive data binding capabilties for working directly with a database in your Windows Forms app. Then I explained why you should never do that in a serious production application and stepped through how to migrate the functionality to a three or four layer/tier architect without giving up any of the capability of the data binding features in Windows Forms.

Here are the slides and demos that I used.



.NET | Community | Data Binding | Languages and Tools

Wednesday, October 12, 2005 8:58:14 PM (GMT Daylight Time, UTC+01:00)
Comments [0]  | 


 # Tuesday, August 30, 2005

Data Binding a ComboBox to a child collection

One of the most frequent questions I get regarding data binding is how to hook up a combo box with a data bound list of selections from a related table or collection, and have selections in that combo box set a corresponding value in the related collection. For example, in the following form, the main collection is the Products table from Northwind. The textboxes are displaying a couple of the columns from that table, and the combo box is displaying the supplier name from the related Suppliers table. Products contains a foreign key column with a SupplierID that makes the link.

Data bound form

The code required to hook all this up in .NET 2.0 is very simple, although maybe slightly difficult to discover on your own. The solution below binds the controls to two BindingSource components, which is the new way of doing data binding in .NET 2.0. The BindingSource component provides a layer of indirection between your bound controls and their data sources that makes synchronizing bound controls easier, allows you to switch out the data source without needing to re-initialize bindings, and exposes a number of useful events to monitor what is going on in bound data sources in your code.

Basically all you need to do is get your data and set that as the data source on the two binding sources. Then set up a normal ComboBox data binding on the ComboBox with the DataSource, DisplayMember, and ValueMember properties. You then add a simple binding to the control's DataBindings collection (an instance of a Binding object) that ties the SelectedValue property on the ComboBox to the corresponding foreign key property/column in the parent collection:

// Retrieve the data for products and suppliers into a data set or custom collections

// Set up the data bindings for the textboxes on the form to their binding source

// i.e. m_ProductNameTextBox.DataBindings.Add("Text", m_ProductsBindingSource, "ProductName", true);

// Set the BindingSource.DataSource property for the textboxes on the form

m_ProductsBindingSource.DataSource = m_Products; // some collection of products

// Set the binding properties for the combo box

m_SuppliersBindingSource.DataSource = m_Suppliers; // some collection of suppliers

m_SuppliersCombo.DataSource = m_SuppliersBindingSource;

m_SuppliersCombo.DisplayMember = "CompanyName";

m_SuppliersCombo.ValueMember = "SupplierID";

// Add a simple binding for the combo box SelectedValue property to the other collection

m_SuppliersCombo.DataBindings.Add("SelectedValue",m_ProductsBindingSource,"SupplierID");

The ComboBox databinding takes care of setting the SelectedValue property on the ComboBox whenever the current item in the parent collection changes.

The steps to regenerate the displayed sample in the Visual Studio 2005 designer are as follows:

  • Create a Database Data Source (typed data set) through the Data Sources window to Northwind with Products and Suppliers tables in it.
  • In the Data Sources window, change the control mapping for the Products table to Details (drop down the list of controls by selecting it and dropping it down)
  • Set the control mapping for the SupplierID column of Products to ComboBox
  • Set the control mapping of other columns you don’t want to display to None.
  • Drag and drop the Products table onto a form. A binding source, binding navigator, the textboxes and the combobox will be generated with their labels.
  • Select the ComboBox in the designer.
  • Go to the Properties grid and select the (DataBindings) property (Advanced) subproperty. Select the ellipses (…) to get the dialog up.
  • The Text property will have a Binding set up for it. Remove that by selecting the Binding drop down and selecting None at the top of the list.
  • Select the SelectedValue property in the Advanced Bindings window and select the productsBindingSource.SupplierID as the bound column. OK out of there.
  • Go back to the Data Sources window and drag the Suppliers table onto the ComboBox. This will hook up the DataSource, DisplayMember, and ValueMember properties.

Hope that is helpful to those groping around with similar data binding scenarios out there.



.NET | Data Binding | Languages and Tools

Tuesday, August 30, 2005 6:22:36 PM (GMT Daylight Time, UTC+01:00)
Comments [0]  | 

Data Binding in Windows Forms 2.0 - Final Manuscript Done!

Phew! After a year and a half of trying to keep pace with the changing betas and writing the book on top of a full schedule of consulting, training, and speaking at conferences and user groups, I am finally done. I submitted the final manuscript to production on Friday. Now I just have to respond to any questions and reviews during the production phase, convert the code samples to VB for download, and I can call this one a wrap.

You can order the book here (available January 2006):

http://www.amazon.com/exec/obidos/ASIN/032126892X/qid=1124482085/sr=11-1/ref=sr_11_1/102-1031358-5664119

We will have a teaser chapter out at PDC that will also be available for download as a PDF containing part of the chapter on the DataGridView control. I'll put up a link to that as soon as it is available.

Now I am start devoting my attention to my next book, Smart Client Deployment with ClickOnce, also part of the Addison Wesley .NET Development series. I hope to knock this one out in the next 6 months, so it should hit the shelves mid 2006.



.NET | Languages and Tools | Publishing | Data Binding | ClickOnce

Tuesday, August 30, 2005 5:50:53 PM (GMT Daylight Time, UTC+01:00)
Comments [0]  | 


















May, 2013 (1)
April, 2013 (2)
March, 2013 (2)
February, 2013 (2)
January, 2013 (2)
December, 2012 (3)
November, 2012 (1)
October, 2012 (1)
August, 2012 (2)
June, 2012 (2)
May, 2012 (3)
April, 2012 (1)
March, 2012 (2)
February, 2012 (2)
January, 2012 (1)
November, 2011 (4)
October, 2011 (1)
September, 2011 (2)
August, 2011 (1)
July, 2011 (1)
May, 2011 (5)
March, 2011 (4)
February, 2011 (2)
January, 2011 (3)
November, 2010 (4)
October, 2010 (1)
September, 2010 (5)
August, 2010 (5)
July, 2010 (6)
June, 2010 (8)
May, 2010 (2)
April, 2010 (2)
January, 2010 (1)
December, 2009 (3)
November, 2009 (2)
October, 2009 (3)
September, 2009 (3)
August, 2009 (2)
July, 2009 (3)
May, 2009 (3)
April, 2009 (2)
March, 2009 (1)
February, 2009 (2)
January, 2009 (2)
December, 2008 (1)
November, 2008 (2)
October, 2008 (5)
September, 2008 (4)
August, 2008 (2)
July, 2008 (1)
June, 2008 (2)
May, 2008 (2)
April, 2008 (3)
February, 2008 (6)
January, 2008 (3)
December, 2007 (1)
November, 2007 (1)
October, 2007 (5)
September, 2007 (1)
July, 2007 (3)
June, 2007 (8)
April, 2007 (2)
March, 2007 (4)
February, 2007 (1)
December, 2006 (2)
November, 2006 (9)
October, 2006 (5)
September, 2006 (3)
August, 2006 (2)
July, 2006 (4)
June, 2006 (5)
May, 2006 (10)
April, 2006 (4)
March, 2006 (2)
February, 2006 (12)
January, 2006 (7)
December, 2005 (2)
November, 2005 (15)
October, 2005 (6)
September, 2005 (7)
August, 2005 (3)
July, 2005 (10)
June, 2005 (11)
May, 2005 (7)
April, 2005 (8)
March, 2005 (6)
February, 2005 (2)
January, 2005 (6)
December, 2004 (3)
November, 2004 (5)
October, 2004 (2)
September, 2004 (5)
August, 2004 (13)
July, 2004 (6)
June, 2004 (14)
May, 2004 (17)
April, 2004 (12)
March, 2004 (8)
February, 2004 (10)
January, 2004 (14)
December, 2003 (9)
November, 2003 (13)
October, 2003 (3)

Sign In
Copyright © 2006-2012 Brian Noyes. All rights reserved.

designed by NUKEATION STUDIOS