Databound Controls

advertisement
Databound Controls
1
Objectives

You will be able to use design time data
binding to display and update SQL Server
database data without writing any C#
code.
2
References



Beginning Visual C# 2005
Karli Watson, et al.
Wrox Press, 2006


Chapter 25
Unchanged in Visual C# 2008
3
Northwind Traders Database




Add the Northwind Traders database
tables to your database on scorpius.
Download SQL script to build tables:
http://www.cse.usf.edu/~turnerr/Software_Systems_Development/
Downloads/
 File add_northwind_tables.sql
Open a command prompt window and run sqlcmd to
execute the script.

See next slide.
4
Execute the Script
5
Check Tables in Server Explorer
6
High Level Components

Data Source


Databound Textbox


Encapsulates Connection and Command
objects.
Automatic updates both directions
DataGridView


Display entire table in grid format
Automatic updates both directions
7
ProductData Application

We will create a new version of the
Northwind Product Browser using high
level components.




SQL Data Source
Databound Textboxes
Databound DataGridView
No C# code!
8
ProductData Application

Create a new C# Windows Forms Application.


Set form1 text property to Product Data


Name: ProductData
DO NOT rename form1
On the following slides we will:

Add a DataSource to the form.


Configure to access Products table.
Add databound textboxes for data.
9
ProductData Application
10
Form1
11
Add a Data Source
On the Data menu, select Add New Data Source.
12
Data Source Configuration Wizard
13
Data Source Configuration Wizard
Select connection to your SQL Server database.
Or create a new connection if necessary.
14
Data Source Configuration Wizard
15
Data Source Configuration Wizard
Expand Tables, then Products. Select columns. Click Finish.
16
DataSet in Solution Explorer
17
Show Data Sources

To add databound controls to the form,
select Show Data Sources on the Data
menu
18
Show Data Sources
19
Adding a DataSet to the Program
Expand Products. Drag ProductName to design surface.
20
Adding a DataSet to the Program
Build and run.
21
App Running
Note navigation
controls.
22
Add Data to Form



Drag other columns to the design surface.
Arrange and resize labels and textboxes.
Build and run
23
Add Data to Form
24
App Running
Note that you can update the data and save it back to the database.
Click the floppy disk icon to save.
End of Section
25
Using a DataGridView


The DataGridView allows us to display an
entire table as a grid.
DataBinding permits all work to be done
at design time.

No C# code required!
26
Using a DataGridView


Create a new project called Product_Grid
Set form1 Text property to Northwind
Products.


Add a data source as before.


DO NOT rename form1.
Same configuration.
Show Data Sources.
27
Using a DataGridView
Drag the Products Table to the design surface.
28
Using a DataGridView
Resize the form and the data grid.
29
Using a DataGridView
Anchor the data grid on all four sides.
Build and run.
30
Products_Datagrid Running
31
Update


Note that we can update data shown on
the grid.
Clicking the “Save” icon will update the
database.
32
Summary

Databound Controls permit us to create an
data driven application with zero code.


Just configure high level components.
Internally these controls use the low level
ADO classes that we saw previously.
33
Download