Presentation is loading. Please wait.

Presentation is loading. Please wait.

4: DataGrid, DataView, and ListView

Similar presentations


Presentation on theme: "4: DataGrid, DataView, and ListView"— Presentation transcript:

1 4: DataGrid, DataView, and ListView
Updated 9/12/2004 Copyright Scott-Jones Publishing, All rights reserved.

2 Overview DataGrid control DataViews and ListViews
Three-Tier Sports Rental Income Sports Rental Checkout Example Command Objects Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 2

3 4.1 DataGrid Control Spreadsheet-like view of a dataset Simple to use
Great for lists of data Easily bound to a dataset Provides “instant output” Easy to customize and configure Provides a convenient way to change data in a data set Can change data simply by keying over it Able to delete existing rows Able to insert new rows Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 3

4 DataGrid Properties Frequently used DataGrid properties:
Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 4

5 Rental Inventory DataGrid
Hands-on tutorial Program: RentalInventory Displays Items table from SportsRentals database in a DataGrid Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 5

6 Rental Inventory Output
Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 6

7 Table and Column Styles
Configure the DataGrid's TableStyles property for each column, set the HeaderText and Width optional: align and format the column data optional: remove row headers, make grid read-only See the RentalInventoryColumns program Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 7

8 Examples Original Data Grid Read Only with Row Selector Removed
Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 8

9 DataGrid Events Click Event
fires when user clicks on row selector use CurrentRowIndex to obtain dataset row: Dim drItem As DataRow = _ DsItems1.Items(dgdInventory.CurrentRowIndex) To delete the selected row of the dataset above drItem.Delete() To update a field in the dataset selected row drItem(“Amount”) = txtAmount.text Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 9

10 Karate Updates Example
Program: KarateUpdates Actions: displays the Payments table user can delete a payment user can select a payment and modify the amount underlying database can be updated Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 10

11 DataGrid Tips Alter field order in data adapter's SELECT statement to change default column order in DataGrid Define table styles and column styles for complete control over DataGrid columns Write code in CurrentCellChanged event to prevent user from modifying a column dgdItems.ReadOnly = _ (dgdItems.CurrentCell.ColumnNumber = 0) Bind grid to dataset in Visual Studio (DataSource prop) Or bind grid to dataset in code dgdPayments.SetDataBinding(DsPayments1, “Payments") -or- dgdPayments.DataSource = dsPayments1 dgdPayments.DataMember = “Payments” Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 11

12 4.2 DataViews and ListViews
Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 12

13 DataView Control Sorts and/or filters a dataset
Drag the DataView control from the Toolbox Data section to a form Important properties: Table: dataset & table containing the data (dsMbrs1.Mbrs) Sort: field (column) name to be sorted (can use DESC) RowFilter: a selection expression such as “Salary>30000” Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 13

14 Accessing a DataRow Accessing LastName column of row 0 of the DataView
txtName.Text = dvMembers(0).Item("LastName") Each row is a DataRowView object Dim a DataRow and get row 0 from the DataView: Dim row As DataRowView = dvMembers(0) Use the DataRow to get contents of the LastName column: txtName.Text = row("LastName") Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 14

15 Filling a ListView Control
Excellent for displaying database tables Very flexible Looks like a DataGrid No data binding Fill from either dataset or DataView Example: Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 15

16 ListView's Structure Items collection holds the rows
Each row is a ListViewItem object In each row: Text property holds first column SubItems collection holds the other columns Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 16

17 Example: Filling a ListView
lvwPayments.Items.Clear() Dim count As Integer = dvPayments.Count Dim i As Integer Dim item As ListViewItem For i = 0 To count - 1 With dvPayments(i) item = New ListViewItem(Format(.Item("PaymentDate"), _ " MM/dd/yyyy")) lvwPayments.Items.Add(item) item.SubItems.Add(.Item("MemberId")) item.SubItems.Add(FormatNumber(.Item("Amount"))) End With Next i Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 17

18 Karate School Payments
Hands-on tutorial Program: KaratePayments Fills a ListView from a DataView Sorts on any column, both directions, when user clicks on column header Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 18

19 4.3 Three-Tier Sports Rental Income
Hands-on tutorial Program: RentalIncome Displays and calculates from the Items table estimates income based on rental rates and inventory Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 19

20 Class Design Implements a Three-Tier model (see Chapter 1)
Data tier – SportsRental class Business tier – Items class application-specific calculations Presentation tier – frmRentalIncome class Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 20

21 4.4 Sports Rental Checkout Example
Program: RentalCheckout Emphasis on object-oriented design Checks out rental items from a Sports Rental store Design tool: Use-case scenario describes interaction between user and program lists anticipated inputs and outcomes Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 21

22 Subtasks User enters a username and password
Clerk creates a Rental Invoice Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 22

23 Subtask: Clerk enters username, password
Program displays a dialog window containing a list of employee names. The clerk selects a name from the list and inputs a corresponding user password. The program checks the password against the database and does one of the following: If the password is correct, the program closes the login window and proceeds to the next subtask. If the password is incorrect, the program displays an error message and asks the clerk to reenter the password. The clerk is given three tries before the program ends. Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 23

24 4.5 Command Objects Visual Studio creates four commands for each data adapter: SelectCommand InsertCommand UpdateCommand DeleteCommand Primary key required for all but SelectCommand Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 24

25 Parameters Collection
Each Command object has a Parameters collection Examples: Reference the collection: daPayments.InsertCommand.Parameters Assign value to a parameter: With daPayments.InsertCommand .Parameters("Date").Value = txtDate.Text End With Get parameter's name: Dim param As OleDbParameter param = daPayments.InsertCommand.Parameters(0) param.ParameterName Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 25

26 OleDbParameter Properties
Direction: Indicates if parameter is input-only, output-only, bidirectional, or a stored procedure return value parameter. IsNullable: Indicates whether parameter accepts null values. Precision: Max digits used to represent Value property. Scale: Number of decimal places to which Value is resolved. Size: Max size, in bytes, of data within column. SourceColumn: Name of source column mapped to DataSet, used for loading or returning value. Value: The value of the parameter. For SQL Server, see the SqlParameter class. Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 26

27 Direct Update Approach
Updates database directly, bypassing dataset Careful: there is no "undo" Other database users get the very latest data Potential for more network traffic when multiple operations execute Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 27

28 Command Object Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 28

29 Karate Commands Example
Program: KarateCommands Displays contents of the Select, Insert, Delete, and Update command objects created by Visual Studio for the daPayments data adapter Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 29

30 Inserting Table Rows General format Example INSERT INTO target
[(field1[,field2[,...]])] VALUES(value1,[,value2[,...]) Example INSERT INTO Payroll (SSN, PaymentDate, HoursWorked, HourlyRate) VALUES(' ', #1/15/1998#, 47.5, 17.50) optional Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 30

31 Query Parameters Make queries more flexible
Avoid messy embedding of variable names inside SQL code Microsoft Access Example: INSERT INTO Payments( Amount, MemberId, PaymentDate ) VALUES (?, ?, ?) SQL Server Example: @PaymentDate) Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 31

32 Executing Commands Open the database connection
Assign value to each parameters Call ExecuteNonQuery Close the connection Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 32

33 InsertCommand Example
Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 33

34 Updating Table Rows Use the WHERE clause, or all rows will be updated!
General format UPDATE table SET fieldname = newvalue [ SET fieldname = newvalue ] ... WHERE criteria Example UPDATE Payroll SET HourlyRate = HourlyRate * 1.05 WHERE PaymentDate > #05/01/1999# Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 34

35 Deleting Table Rows Use the WHERE clause, or all rows will be deleted!
General format DELETE FROM table WHERE criteria Example DELETE FROM Payroll WHERE PaymentDate < #1/1/1998# Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 35

36 Inserting Karate Payments
Example program: KarateInsertPayments Uses a Command object to execute an INSERT query Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 36

37 The End Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 37


Download ppt "4: DataGrid, DataView, and ListView"

Similar presentations


Ads by Google