4: DataGrid, DataView, and ListView

Slides:



Advertisements
Similar presentations
17. Data Access ADO.Net Architecture New Features of ADO.NET
Advertisements

CC SQL Utilities.
1.NET Web Forms DataGrid © 2002 by Jerry Post. 2 Data Grid Has Many Uses  The grid uses HTML tables to display multiple rows of data. It is flexible.
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
VB.NET Database Access ISYS546. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
VB.NET Database Access ISYS 812. Microsoft Universal Data Access ODBC: Open Database Connectivity –A driver manager –Used for relational databases OLE.
1 Pertemuan 09 Database Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
Chapter 8 Binding Data to Web Controls. ASP.NET 2.0, Third Edition2.
Chapter 9 Using the SqlDataSource Control. References aspx.
Databases and LINQ Visual Basic 2010 How to Program 1.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
XP New Perspectives on Microsoft Access 2002 Tutorial 51 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances.
Chapter 15: Using LINQ to Access Data in C# Programs.
Advanced Visual Basic th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.
CIS 451: Using ASP.NET Objects with SQL Dr. Ralph D. Westfall February, 2009.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Neal Stublen Populating a Database  SQLExpress should be installed with Visual Studio  The book provides a.sql file for populating.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 30 – Bookstore Application: Client Tier Examining.
Tutorial 7 Creating Forms. Objectives Session 7.1 – Create an HTML form – Insert fields for text – Add labels for form elements – Create radio buttons.
1 Working with MS SQL Server Textbook Chapter 14.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 15 Advanced Tables.
Copyright 2007, Paradigm Publishing Inc. ACCESS 2007 Chapter 3 BACKNEXTEND 3-1 LINKS TO OBJECTIVES Modify a Table – Add, Delete, Move Fields Modify a Table.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Task #1 Create a relational database on computers in computer classroom 308, using MySQL server and any client. Create the same database, using MS Access.
1 Database Programming with ADO.NET Kashef Mughal.
Module 2: Using ADO.NET to Access Data. Overview ADO.NET Architecture Creating an Application That Uses ADO.NET to Access Data Changing Database Records.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Binding to ListBox and ComboBox Controls List and combo boxes are frequently.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
DB Implementation: MS Access Forms. MS Access Forms: Purpose Data entry, editing, & viewing data in Tables Forms are user-friendlier to end-users than.
To play, start slide show and click on circle Access 1 Access 2 Access 3 Access 4 Access Access
2 Copyright © 2008, Oracle. All rights reserved. Building the Physical Layer of a Repository.
ASP.NET Programming with C# and SQL Server First Edition
Copyright Scott-Jones Publishing, All rights reserved.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Visual Basic 2010 How to Program
Creating Oracle Business Intelligence Interactive Dashboards
ADO.NET Framework.
SQL and SQL*Plus Interaction
Analyzing Data with Excel
DB Implementation: MS Access Forms
VB.NET Using Database.
Database Queries.
MODULE 7 Microsoft Access 2010
CIS16 Application Programming with Visual Basic
DB Implementation: MS Access Forms
Microsoft Official Academic Course, Access 2016
CIS16 Application Programming with Visual Basic
Chapter 10 ADO.
Database Applications
Introduction to Access
Using SQL*Plus.
PROG Advanced Web Apps 4/13/2019 Programming Data Pages Wendi Jollymore, ACES.
Guidelines for Microsoft® Office 2013
Exploring Microsoft® Office 2016 Series Editor Mary Anne Poatsy
Query-by-Example Transparencies
Assignment resource Working with Excel Tables, PivotTables, and Pivot Charts Fairhurst pp The commands on these slides work with the Week 2 Excel.
New Perspectives on Microsoft
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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: VALUES (@Amount, @MemberId, @PaymentDate) Advanced Visual Basic .NET (Irvine, Liang, Gaddis) Slide 31

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

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

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

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

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

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