1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and...

33
1 Databound Controls

Transcript of 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and...

Page 1: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

1

Databound Controls

Page 2: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

2

Objectives

You will be able to use design time data binding to display and update SQL Server database data without writing any C# code.

Page 3: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

3

References

Beginning Visual C# 2005 Karli Watson, et al. Wrox Press, 2006

Chapter 25

Unchanged in Visual C# 2008

Page 4: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

4

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_Develo

pment/Downloads/ File add_northwind_tables.sql

Open a command prompt window and run sqlcmd to execute the script.

See next slide.

Page 5: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

5

Execute the Script

Page 6: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

6

Check Tables in Server Explorer

Page 7: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

7

High Level Components

Data Source Encapsulates Connection and

Command objects.

Databound Textbox Automatic updates both directions

DataGridView Display entire table in grid format Automatic updates both directions

Page 8: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

8

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!

Page 9: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

9

ProductData Application

Create a new C# Windows Forms Application.

Name: ProductData

Set form1 text property to Product Data 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.

Page 10: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

10

ProductData Application

Page 11: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

11

Form1

Page 12: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

12

Add a Data SourceOn the Data menu, select Add New Data Source.

Page 13: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

13

Data Source Configuration Wizard

Page 14: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

14

Data Source Configuration Wizard

Select connection to your SQL Server database.

Or create a new connection if necessary.

Page 15: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

15

Data Source Configuration Wizard

Page 16: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

16

Data Source Configuration Wizard

Expand Tables, then Products. Select columns. Click Finish.

Page 17: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

17

DataSet in Solution Explorer

Page 18: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

18

Show Data Sources

To add databound controls to the form, select Show Data Sources on the Data menu

Page 19: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

19

Show Data Sources

Page 20: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

20

Adding a DataSet to the Program

Expand Products. Drag ProductName to design surface.

Page 21: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

21

Adding a DataSet to the Program

Build and run.

Page 22: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

22

App Running

Note navigation controls.

Page 23: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

23

Add Data to Form

Drag other columns to the design surface.

Arrange and resize labels and textboxes.

Build and run

Page 24: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

24

Add Data to Form

Page 25: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

25

App Running

End of Section

Note that you can update the data and save it back to the database.

Click the floppy disk icon to save.

Page 26: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

26

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!

Page 27: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

27

Using a DataGridView

Create a new project called Product_Grid

Set form1 Text property to Northwind Products.

DO NOT rename form1.

Add a data source as before. Same configuration.

Show Data Sources.

Page 28: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

28

Using a DataGridView

Drag the Products Table to the design surface.

Page 29: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

29

Using a DataGridView

Resize the form and the data grid.

Page 30: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

30

Using a DataGridView

Anchor the data grid on all four sides. Build and run.

Page 31: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

31

Products_Datagrid Running

Page 32: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

32

Update

Note that we can update data shown on the grid.

Clicking the “Save” icon will update the database.

Page 33: 1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.

33

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.