Data Binding


Database-centric applications are fully supported in WinForms. To get started, you can use Server Explorer to add connections to whatever databases you'd like. For example, Figure 1.18 shows Server Explorer and the tables in a database maintained on a popular Windows developer resource site.

Figure 1.18. A Database Connection in Server Explorer

Dragging a table from Server Explorer onto a Designer surface creates two components : a connection to connect to the database, and an adapter to shuttle data back and forth across the connection. Right-clicking on the adapter in the Designer and choosing Generate Dataset allows you to create a new data set , a DataSet-derived class specially generated to hold data for the table you originally dragged from Server Explorer. The default General Dataset options will also create an instance of the new data set for you to associate with controls.

Associating a source of data, such as a data set, with one or more controls is known as data binding . Binding a control to a data source provides for bidirectional communication between the control and the data source so that when the data is modified in one place, it's propagated to the other. Several data bound controls are provided with WinForms, including ListBox and ComboBox, but of all of them, the DataGrid control is the most flexible. Figure 1.19 shows a form with a data grid bound to the data set that's already been created.

Figure 1.19. A DataGrid Bound to a Data Set

When there's a data set on the form, it's easy to bind the data grid to it. You set the data grid's DataSource property in the Property Browser and fill the data set when the form is loaded. To do this, you use the data adapter:

 void DownloadsForm_Load(object sender, EventArgs e) {  sqlDataAdapter1.Fill(downloadsDataSet1);  } 

This is only a scratch on the surface of what can be done with data binding in general and the data grid specifically . For more information, read Chapter 12: Data Sets and Designer Support, and Chapter 13: Data Binding and Data Grids.



Windows Forms Programming in C#
Windows Forms Programming in C#
ISBN: 0321116208
EAN: 2147483647
Year: 2003
Pages: 136
Authors: Chris Sells

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net