Viewing.NET Data Asad Usman Khan Syed Amier Haider Rehan Ahmad.

Slides:



Advertisements
Similar presentations
Mobile Development Introduction to Visual Studio Development Rob Miles Department of Computer Science.
Advertisements

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.
Using Macros and Visual Basic for Applications (VBA) with Excel
EXCEL Spreadsheet Basics
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Introduction to Data Adapter. A Simplified View of ADO.Net Objects Ado.Net Data Provider Connection Adapter Command Reader Dataset Data Consumer WinForm.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Microsoft Visual Basic 2005 ENRICHMENT CHAPTER Visual Studio Tools for Office.
© 2002 ComputerPREP, Inc. All rights reserved. Word 2000: Working with Long Documents.
McGraw-Hill© 2007 The McGraw-Hill Companies, Inc. All rights reserved. 1-1.
8 Copyright © 2004, Oracle. All rights reserved. Creating LOVs and Editors.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Databases and LINQ Visual Basic 2010 How to Program 1.
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.
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 71 Building Data-Driven ASP.NET Applications Introduction to ASP.NET By Kathleen Kalata.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
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.
Domain 3 Understanding the Adobe Dreamweaver CS5 Interface.
Connecting to Data Sources Using ADO.NET Dr. Awad Khalil Computer Science & Engineering Department AUC.
Working with GridView Control: Adding Columns. Adding Buttons to a Bound GridView: 1. Drag the WebProduct table from Data connection to a page 2. Demo.
CSCI 6962: Server-side Design and Programming Database Manipulation in ASP.
ASP.NET Rina Zviel-Girshin Lecture 5
Session 8: ADO.NET. Overview Overview of ADO.NET What is ADO.NET? Using Namespaces The ADO.NET Object Model What is a DataSet? Accessing Data with ADO.NET.
Using Adapter Wizard ISYS 512. Data Adapter Wizard – 2 nd Level of Using ADO.Net Configure Data Adapter and generating a dataset: –From the Data tab of.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
C# GUI - Basics. Objectives.NET supports two types: WinForms, traditional, desktop GUI apps. WebForms – newer, for Web apps. Visual Studio.NET supports.
Chapter 2 – Introduction to the Visual Studio .NET IDE
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
ATN GIS Support ArcGIS: ArcCatalog.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
TRAINING SESSIONS.NET Controls.  Standard Controls  Label  Textbox  Checkbox  Button, Image Button, Image control  Radio Button  Literal  Hyperlink.
Graphical Enablement In this presentation… –What is graphical enablement? –Introduction to newlook dialogs and tools used to graphical enable System i.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to the Visual Studio.NET IDE Outline 2.1Introduction 2.2Visual Studio.NET Integrated.
1 Database Programming with ADO.NET Kashef Mughal.
Chapter 2: Excel Basics and Formatting Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Effective C# 50 Specific Way to Improve Your C# Item 22, 23.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Understand Databound Controls Windows Development Fundamentals LESSON 4.2A.
DataGridView. Displaying data in a tabular format is a task you are likely to perform frequently. The DataGridView control is designed to be a complete.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Customizing Menus and Toolbars CHAPTER 12 Customizing Menus and Toolbars.
 2002 Prentice Hall. All rights reserved. 1 Introduction to the Visual Studio.NET IDE Outline Introduction Visual Studio.NET Integrated Development Environment.
제 21 장 : ADO.NET 을 이용한 데이터 접근. ADO.NET - DataSet 사용하기.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
DATA GRID VIEW objective of lecture:- After completing this lecture, you will be able to: Create a data grid view object on a windows form and use it to.
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
Chapter 1: An Introduction to Visual Basic .NET
Visual Basic 2010 How to Program
Chapter 9 Programming Based on Events
Chapter 2 – Introduction to the Visual Studio .NET IDE
Signature: Microsoft Word 2003
Chapter 2 – Introduction to the Visual Studio .NET IDE
Brief description on how to navigate within this presentation (ppt)
CIS16 Application Programming with Visual Basic
Database Applications
Chapter 10 Accessing Database Files
Presentation transcript:

Viewing.NET Data Asad Usman Khan Syed Amier Haider Rehan Ahmad

Outline… The DataGrid Control –Displaying Tabular Data –Data Sources Displaying Data from a Array Data Table Displaying Data from a DataView Displaying Data from a DataSet IListSource and IList Interfaces –DataGrid Class Hierarchy DataGridTableStyle and DataGridColumnStyle 2

…Outline Data Binding –Simple Binding –Data Binding Objects Visual Studio and Data Access –Creating a Connection –Selecting Data –Generating a Dataset –Updating the Data Source –Building a Schema Adding an Element –Other Common Requirments Manufactured Tables and Rows Using an Attribute Dispatching Methods Getting the Selected Row 3

THE DATAGRID CONTROL 4

The DataGrid Control One of the best features of the new DataGrid control is its flexibility The data source can be an Array, DataTable, DataView, DataSet class, or a component that implements either the IListSource or IList interface. The DataGrid control gives you a variety of views of the same data Data can be displayed (as in a DataSet class) by calling the SetDataBinding() method. 5

Displaying Tabular Data The data can also be displayed through 6

To Create main window and instance variables using System; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; public class DisplayTabularData : System.Windows.Forms.Form { private System.Windows.Forms.Button retrieveButton; private System.Windows.Forms.DataGrid dataGrid; public DisplayTabularData() { this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(464, 253); this.Text = “01_DisplayTabularData”; 7

Creation of data grid Control this.dataGrid = new System.Windows.Forms.DataGrid(); dataGrid.BeginInit(); dataGrid.Location = new System.Drawing.Point(8, 8); dataGrid.Size = new System.Drawing.Size(448, 208); dataGrid.TabIndex = 0; dataGrid.Anchor = AnchorStyles.Bottom | AnchorStyles.Top |AnchorStyles.Left | AnchorStyles.Right; this.Controls.Add(this.dataGrid); dataGrid.EndInit(); 8

Creating a Button this.retrieveButton = new System.Windows.Forms.Button(); retrieveButton.Location = new System.Drawing.Point(384, 224); retrieveButton.Size = new System.Drawing.Size(75, 23); retrieveButton.TabIndex = 1; retrieveButton.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; retrieveButton.Text = “Retrieve”; retrieveButton.Click += new System.EventHandler (this.retrieveButton_Click); this.Controls.Add(this.retrieveButton); } 9

Associating Event protected void retrieveButton_Click(object sender, System.EventArgs e) { retrieveButton.Enabled = false; string source = “server=(local)\\NetSDK;” + “uid=QSUser;pwd=QSPassword;” + “database=Northwind”; string select = “SELECT * FROM Customers” ; SqlConnection conn = new SqlConnection(source); SqlDataAdapter da = new SqlDataAdapter( select, conn); DataSet ds = new DataSet(); da.Fill(ds, “Customers”); dataGrid.SetDataBinding(ds, “Customers”); } static void Main() { Application.Run(new DisplayTabularData()); } 10

Data Sources… The DataGrid control provides a flexible way to display data; in addition to calling SetDataBinding() with a DataSet and the name of the table to display, this method can be called with any of the following data sources: 11

…Data Sources An array (the grid can bind to any one dimensional array) DataTable DataView DataSet or DataViewManager Components that implement the IListSource Interface Components that implement the IList interface 12

Displaying data from an array string[] stuff = new string[] {“One”, “Two”, “Three”}; dataGrid.SetDataBinding(stuff, null); protected class Item { public Item(string text) { _text = text; } public string Text { get{return _text;} } private string _text; } 13

DataTable There are two ways to display a DataTable within a DataGrid control: If your DataTable is standalone, call SetDataBinding(DataTable, null) If your DataTable is contained within a DataSet, call SetDataBinding(DataSet,“ ”) 14

Displaying data from a DataView A DataView provides a means to filter and sort data within a DataTable. A DataView does not permit filtering of columns only rows. DataView dv = new DataView(dataTable); 15

Setting AllowEdit = false disables all column edit functionality for rows Setting AllowNew = false disables the new row functionality Setting AllowDelete = false disables the delete row capability Setting the RowStateFilter displays only rows of a given state Setting the RowFilter enables you to filter rows Sorting the rows by certain columns 16

Filtering rows by data 17

Filtering rows on state 18

Sorting rows dataView.Sort = “ProductName”; dataView.Sort = “ProductName ASC, ProductID DESC”; 19

Displaying data from a DataSet class SqlDataAdapter da = new SqlDataAdapter(orders, conn); DataSet ds = new DataSet(); da.Fill(ds, “Orders”); da = new SqlDataAdapter(customers, conn); da.Fill(ds, “Customers”); ds.Relations.Add(“CustomerOrders”, ds.Tables[“Customers”].Columns[“CustomerID”], ds.Tables[“Orders”].Columns[“CustomerID”]); Once created, the data in the DataSet is bound to the DataGrid simply by calling SetDataBinding(): dataGrid1.SetDataBinding(ds, “Customers”); 20

Displaying data in a data view manager The display of data in a DataViewManager is the same as that for the DataSet. However, when a DataViewManager is created for a DataSet, an individual DataView is created for each DataTable, which then permits the code to alter the displayed rows, based on a filter or the row state. 21

Displaying data in a data view manager DataViewManager dvm = new DataViewManager(ds); dvm.DataViewSettings[“Customers”].RowFilter = “Country=’UK’”; dataGrid.SetDataBinding(dvm, “Customers”); 22

IListSource and IList interfaces The DataGrid also supports any object that exposes one of the interfaces IListSource or IList. IListSource only has one method, GetList(), which returns an IList interface. IList on the other hand is somewhat more interesting, and is implemented by a large number of classes in the runtime. Some of the classes that implement this interface are Array, ArrayList, and StringCollection. When using IList, the same caveat for the object within the collection holds true as for the Array implementation shown earlier—if a StringCollection is used as the data source for the DataGrid, the length of the strings is displayed within the grid, not the text of the item as expected. 23

DataGrid Class Hierarchy 24

DataGridTableStyle A DataGridTableStyle contains the visual representation of a DataTable. The DataGrid contains a collection of these styles, accessible by the TableStyles property. The DataGridTableStyle permits the definition of the visual parameters for the DataGrid, such as the background and foreground color, the font used in the column header, and various other properties. 25

DataGridColumnStyle The DataGridColumnStyle can be used to refine the display options on a column-by- column basis, such as setting the alignment for the data in the column, the text that is displayed for a null value, and the width of the column on screen. 26

Data Binding The process of linking a control to a data source is called data binding. The facilities available within.NET for binding data to controls is substantially easier to use and also more capable. For example, in.NET you can bind data to most properties of a control, not just the text property. 27

Simple Binding A control that supports single binding typically displays only a single value at once, such as a text box or radio button. The following example shows how to bind a column from a DataTable to a TextBox: DataSet ds = CreateDataSet(); textBox1.DataBindings.Add(“Text”, ds, “Products.ProductName”); 28

Data-Binding Objects… 29

…Data-Binding Objects 30

VISUAL STUDIO.NET AND DATA ACCESS 31

Visual Studio.NET and Data Access Creating a Connection Selecting Data Generating a Dataset Updating the Data Source Building a Schema –Adding an Element Other Common Requirments –Manufactured Tables and Rows –Using an Attribute –Dispatching Methods –Getting the Selected Row 32

Creating a Connection… Step 1Step 2 33

Step 3 this.sqlConnection = new System.Data.SqlClient.SqlConnection(); // // sqlConnection // this.sqlConnection.ConnectionString = “data source=(local)\\NETSDK;” + “initial catalog=Northwind;” + “user id=QSUser;password=QSPassword;” + “persist security info=True;” + “workstation id=BILBO;” + “packet size=4096”; 34

Selecting Data Simply Drag and drop the table on the form. The code is generated automatically but is very redundant. 35

Generating a DataSet… To generate the DataSet, select the data adapter and display its properties Towards the bottom of the property sheet there are three options: Configure Data Adapter, Generate Dataset, and Preview Data. 36

…Generating a DataSet… Select Generate DataSet. You will be prompted to provide a name for the new DataSet object before you can choose the tables that you want to add to the data set. If you have dragged multiple tables from Server Explorer and dropped them onto the form, you can link them from inside the dialog box to a single DataSet. What is actually created is an XSD schema, defining the DataSet and each table that was included in the DataSet. 37

…Generating a DataSet… In addition to the XSD file there is a (hidden).cs file that defines various type-safe classes. To view this file, click the Show All Files toolbar button and then expand the XSD file. 38

…Generating a DataSet Visual Studio.NET creates a.cs file with the same name as the XSD file. The classes defined are as follows: –A class derived from DataSet –A class derived from DataTable for the data adapter chosen –A class derived from DataRow, defining the columns accessible within the DataTable –A class derived from EventArgs, used when a row changes 39

Updating the Data Source… private void retrieveButton_Click(object sender, System.EventArgs e) { // Fill the data adapter from the database supplierDataAdapter.Fill ( supplierDataSet, “Supplier” ) ; // And display the data in the data grid... dataGrid1.SetDataBinding ( supplierDataSet, “Supplier” ) ; // And disable the retrieve button... retrieveButton.Enabled = false ; } 40

…Updating the Data Source private void updateButton_Click(object sender, System.EventArgs e) { // Update the database int modified = supplierDataAdapter.Update ( supplierDataSet, “Supplier” ) ; if ( modified > 0 ) MessageBox.Show ( string.Format ( “Modified {0} rows”, modified ) ) ; } 41

Building a Schema… Add New Item from the Proejct menu Select the XML Schema item from the Data category Name your schema 42

…Building a Schema 43

…Building a Schema 44

Adding an element To add a new top-level element, right-click inside your workspace and Choose Add ➪ New Element from the context menu. 45

Other Common Requirements A common requirement when displaying data is to provide a pop-up menu for a given row. When the user right-clicks on any part of a row in the DataGrid, the row is looked up to check if it derives from ContextDataRow, and if so, PopupMenu()can be called. This could be implemented using an interface; however, in this instance a base class provides a simpler solution. 46

Other Common Requirements… ContextMenu menu = new ContextMenu(); foreach (MethodInfo meth in members) { //... Add the menu item } System.Drawing.Point pt = new System.Drawing.Point(x,y); menu.Show(parent, pt); 47

Manufactured tables and rows… public class CustomerTable : DataTable { public CustomerTable() : base(“Customers”) { this.Columns.Add(“CustomerID”, typeof(string)); this.Columns.Add(“CompanyName”, typeof(string)); this.Columns.Add(“ContactName”, typeof(string)); } protected override System.Type GetRowType() { return typeof(CustomerRow); } protected override DataRow NewRowFromBuilder(DataRowBuilder builder) { return(DataRow) new CustomerRow(builder); } 48

…Manufactured tables and rows The first prerequisite of a DataTable is to override the GetRowType() method. This is used by the.NET internals when generating new rows for the table. The type used to represent each row should be returned from this method. The next prerequisite is to implement NewRowFromBuilder(), which is called by the runtime when creating new rows for the table. That’s enough for a minimal implementation. This implementation includes adding columns to the DataTable. 49

Black List public class CustomerRow : ContextDataRow { public CustomerRow(DataRowBuilder builder) : base(builder) { } public string CustomerID { get { return (string)this[“CustomerID”];} set { this[“CustomerID”] = value;} } // Other properties omitted for clarity [ContextMenu(“Blacklist Customer”)] public void Blacklist() { // Do something } [ContextMenu(“Get Contact”,Default=true)] public void GetContact() { // Do something else } 50

Using an attribute The idea behind writing the ContextMenu attribute is to be able to supply a free text name for a given menu option. There are a number of other members that could be added to this attribute, including: –A hotkey for the menu option –An image to be displayed –Some text to be displayed in the toolbar as the mouse pointer rolls over the menu option –A help context ID 51

[AttributeUsage(AttributeTargets.Method,AllowMultiple=f alse,Inherited=true)] public class ContextMenuAttribute : System.Attribute { public ContextMenuAttribute(string caption) { Caption = caption; Default = false; } public readonly string Caption; public bool Default; } 52

Dispatching methods When a menu is displayed in.NET, each menu option is linked to the processing code for that option by means of a delegate. In implementing the mechanism for connecting menu choices to code, you have two options: Implement a method with the same signature as the System.EventHandler. This is defined as shown in this snippet: public delegate void EventHandler(object sender, EventArgs e); Define a proxy class, which implements the above delegate, and forwards calls to the received class. This is known as the Command pattern 53

public class MenuCommand { public MenuCommand(object receiver, MethodInfo method) { Receiver = receiver; Method = method; } public void Execute(object sender, EventArgs e) { Method.Invoke(Receiver, new object[] {} ); } public readonly object Receiver; public readonly MethodInfo Method; } 54

Dispatching methods 55

Getting the selected row Given the data source, a data member, and the binding context of the form, the current row can be located with the following code: protected void dataGrid_MouseUp(object sender, MouseEventArgs e) { // Perform a hit test if(e.Button == MouseButtons.Right) { // Find which row the user clicked on, if any DataGrid.HitTestInfo hti = dataGrid.HitTest(e.X, e.Y); // Check if the user hit a cell if(hti.Type == DataGrid.HitTestType.Cell) { // Find the DataRow that corresponds to the cell //the user has clicked upon 56

BindingManagerBase bmb = this.BindingContext[ dataGrid.DataSource, dataGrid.DataMember]; bmb.Position = hti.Row; DataRowView drv = bmb.Current as DataRowView; if(drv != null) { ContextDataRow ctx = drv.Row as ContextDataRow; if(ctx != null) ctx.PopupMenu(dataGrid,e.X,e.Y); } 57

ARIGATOU GOZAIMASU!! Questions? 58