- •Contents
- •What Is C#?
- •C# Versus Other Programming Languages
- •Preparing to Program
- •The Program Development Cycle
- •Your First C# Program
- •Types of C# Programs
- •Summary
- •Workshop
- •C# Applications
- •Basic Parts of a C# Application
- •Structure of a C# Application
- •Analysis of Listing 2.1
- •Object-Oriented Programming (OOP)
- •Displaying Basic Information
- •Summary
- •Workshop
- •Variables
- •Using Variables
- •Understanding Your Computer’s Memory
- •C# Data Types
- •Numeric Variable Types
- •Literals Versus Variables
- •Constants
- •Reference Types
- •Summary
- •Workshop
- •Types of Operators
- •Punctuators
- •The Basic Assignment Operator
- •Mathematical/Arithmetic Operators
- •Relational Operators
- •Logical Bitwise Operators
- •Type Operators
- •The sizeof Operator
- •The Conditional Operator
- •Understanding Operator Precedence
- •Converting Data Types
- •Understanding Operator Promotion
- •For Those Brave Enough
- •Summary
- •Workshop
- •Controlling Program Flow
- •Using Selection Statements
- •Using Iteration Statements
- •Using goto
- •Nesting Flow
- •Summary
- •Workshop
- •Introduction
- •Abstraction and Encapsulation
- •An Interactive Hello World! Program
- •Basic Elements of Hello.cs
- •A Few Fundamental Observations
- •Summary
- •Review Questions
- •Programming Exercises
- •Introduction
- •Essential Elements of SimpleCalculator.cs
- •A Closer Look at SimpleCalculator.cs
- •Simplifying Your Code with Methods
- •Summary
- •Review Questions
- •Programming Exercises
- •Introduction
- •Lexical Structure
- •Some Thoughts on Elevator Simulations
- •Concepts, Goals and Solutions in an Elevator Simulation Program: Collecting Valuable Statistics for Evaluating an Elevator System
- •A Deeper Analysis of SimpleElevatorSimulation.cs
- •Class Relationships and UML
- •Summary
- •Review Questions
- •Programming Exercises
- •The Hello Windows Forms Application
- •Creating and Using an Event Handler
- •Defining the Border Style of the Form
- •Adding a Menu
- •Adding a Menu Shortcut
- •Handling Events from Menus
- •Dialogs
- •Creating Dialogs
- •Using Controls
- •Data Binding Strategies
- •Data Binding Sources
- •Simple Binding
- •Simple Binding to a DataSet
- •Complex Binding of Controls to Data
- •Binding Controls to Databases Using ADO.NET
- •Creating a Database Viewer with Visual Studio and ADO.NET
- •Resources in .NET
- •Localization Nuts and Bolts
- •.NET Resource Management Classes
- •Creating Text Resources
- •Using Visual Studio.NET for Internationalization
- •Image Resources
- •Using Image Lists
- •Programmatic Access to Resources
- •Reading and Writing RESX XML Files
- •The Basic Principles of GDI+
- •The Graphics Object
- •Graphics Coordinates
- •Drawing Lines and Simple Shapes
- •Using Gradient Pens and Brushes
- •Textured Pens and Brushes
- •Tidying up Your Lines with Endcaps
- •Curves and Paths
- •The GraphicsPath Object
- •Clipping with Paths and Regions
- •Transformations
- •Alpha Blending
- •Alpha Blending of Images
- •Other Color Space Manipulations
- •Using the Properties and Property Attributes
- •Demonstration Application: FormPaint.exe
- •Why Use Web Services?
- •Implementing Your First Web Service
- •Testing the Web Service
- •Implementing the Web Service Client
- •Understanding How Web Services Work
- •Summary
- •Workshop
- •How Do Web References Work?
- •What Is UDDI?
- •Summary
- •Workshop
- •Passing Parameters and Web Services
- •Accessing Data with Web Services
- •Summary
- •Workshop
- •Managing State in Web Services
- •Dealing with Slow Services
- •Workshop
- •Creating New Threads
- •Synchronization
- •Summary
- •The String Class
- •The StringBuilder Class
- •String Formatting
- •Regular Expressions
- •Summary
- •Discovering Program Information
- •Dynamically Activating Code
- •Reflection.Emit
- •Summary
- •Simple Debugging
- •Conditional Debugging
- •Runtime Tracing
- •Making Assertions
- •Summary
Data Bound Controls
289
CHAPTER 3.3
LISTING 3.3.2 Continued
216:{
217:Application.Run(new datasetapp());
218:}
219:}
220:}
Let’s examine Listing 3.3.2 in more detail. The main initialization of the DataSet takes place on lines 73–88. Here, a DataSet is created and a new table placed in its tables collection. The table is given a set of columns that are identified by the strings “First”, “Name”, and so on.
Adding data to a table is done one row at a time. The method on lines 57–69 adds a new row to the end of the table and optionally fills it with text. You could also create a table with numeric values or other types.
Lines 92–99 do the binding of the data in the table to the Text property of the identified controls. These controls can now be used to edit the information in the table, and they will display each entry automatically as you navigate through the database.
The button handlers that do the navigation are on lines 24–37. These use the ContextManager.Position property to index the rows in the table.
Figure 3.3.2 shows a screenshot of the application.
FIGURE 3.3.2
The DataSet simple binding application.
Complex Binding of Controls to Data
Simple binding is the process of tying a single control to a single data item. Complex binding ties a single control to many data items. Controls that are often used for complex data binding include DataGrid which has the appearance of a simple spreadsheet. This control can display, edit, or add and delete records from a table. A single databinding operation is used to attach the
3.3
OUNDB ATAD
ONTROLSC
Windows Forms
290
PART III
control to a DataTable. The DataGrid can only be used to show one table at a time, but it can be used to show relationships between tables.
Listing 3.3.3 shows a modified version of the simple binding example demonstrated in Listing 3.3.2. This listing removes all the controls associated with the DataSet and replaces them with a complex bound grid control. Don’t be put off by the title. Complex data binding is complex only in its function, not its usage.
LISTING 3.3.3 gridbind.cs: An Example of Complex Data Binding
1:namespace Sams
2:{
3:using System;
4:using System.Drawing;
5:using System.Collections;
6:using System.Data;
7:using System.ComponentModel;
8:using System.Windows.Forms;
10://This application shows simple data binding to a
11://programatically created dataset.
12:public class GridBind : System.Windows.Forms.Form
13:{
14:private System.ComponentModel.Container components;
15:private System.Windows.Forms.DataGrid dataGrid1;
17://The dataset used to store the table
18:private DataSet dataset;
19:
20://Called at creation to initialize the
21://dataset and create an empty table
22:private void InitDataSet()
23:{
24:dataset = new DataSet(“ContactData”);
26: DataTable t=new DataTable(“Contacts”); 27:
28:t.Columns.Add(“First”,typeof(System.String));
29:t.Columns.Add(“Name”,typeof(System.String));
30:t.Columns.Add(“Company”,typeof(System.String));
31:t.Columns.Add(“Title”,typeof(System.String));
32:t.Columns.Add(“Phone”,typeof(System.String));
34: t.MinimumCapacity=100; 35:
Data Bound Controls
CHAPTER 3.3
LISTING 3.3.3 Continued
36:dataset.Tables.Add(t);
37:}
38:
39://Called at initialization to do complex binding of the DataGrid
40://to the dataset’s “Contacts” table entries
41:private void BindGrid()
42:{
43:this.dataGrid1.SetDataBinding(dataset.Tables[“Contacts”],””);
44:}
45:
46://Constructor. Positions the form controls,
47://Ininitializes the dataset, binds the controls and
48://wires up the handlers.
49:public GridBind()
50:{
51:InitializeComponent();
52:
53: InitDataSet(); 54:
55: BindGrid(); 56:
57: } 58:
59://Cleans up the Form
60:public override void Dispose()
61:{
62:base.Dispose();
63:components.Dispose();
64:}
65:
66://Method added by the form designer
67:private void InitializeComponent()
68:{
69:this.components = new System.ComponentModel.Container ();
70:this.dataGrid1 = new System.Windows.Forms.DataGrid ();
71:dataGrid1.BeginInit ();
72:dataGrid1.Location = new System.Drawing.Point (8, 16);
73:dataGrid1.Size = new System.Drawing.Size (472, 224);
74:dataGrid1.DataMember = “”;
75:dataGrid1.TabIndex = 0;
76:this.Text = “GridBind”;
77:this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
78:this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
79:this.ClientSize = new System.Drawing.Size (486, 251);
291
3.3
OUNDB ATAD
ONTROLSC
Windows Forms
292
PART III
LISTING 3.3.3 Continued
80:this.Controls.Add (this.dataGrid1);
81:dataGrid1.EndInit ();
82:}
83:
84:
85:
86://Main method to instantiate and run the application.
87:static void Main()
88:{
89:Application.Run(new GridBind());
90:}
91:}
92:}
Compile this program with the following command line:
csc /t:winexe gridbind.cs
Looking at the information in Listing 3.3.3, you can see that the code used to navigate the data set is no longer needed. The method to add a new row of data is not needed either. The DataGrid control does all that for you. Binding the data to the grid takes place on line 43. Initialization of the data set is identical to the previous example; it is shown on lines 22–37. Figure 3.3.3 shows the GridBind application in action. Notice that the grid provides column and row headers that allow you to select a whole row or order the table according to column alphabetic order.
FIGURE 3.3.3
Complex binding to a DataGrid.
The DataGrid control can also be used to show hierarchical relationships between multiple data tables. Perhaps a Customer Resource Management application would want to show the relationship between a customer and his or her orders or a client and his or her technical support incidents. Complex binding of data makes this otherwise difficult process painless.