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.

Slides:



Advertisements
Similar presentations
Coding ADO.Net DataSet Objects ISYS 512. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set.
Advertisements

Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
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.
Introduction to Database Processing with ADO.NET.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
Using ADO.NET Chapter Microsoft Visual Basic.NET: Reloaded 1.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 2 Introduction to Visual C#
Coding ADO.Net DataSet Objects. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set of disconnedted.
Introduction to ADO.Net and Visual Studio Database Tools
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
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.
ADO.NET By Hanumantha Rao.N MCA By Hanumantha Rao.N MCA.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 81 Building Data-Driven Web Applications Introduction to ASP.NET By Kathleen Kalata.
Chapter 71 Building Data-Driven ASP.NET Applications Introduction to ASP.NET By Kathleen Kalata.
Data Binding to Controls Programming in C# Data Binding to Controls CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
CIS 375—Web App Dev II ASP.NET 10 Database 2. 2 Introduction to Server-Side Data Server-side data access is unique in that Web pages are basically ___________.
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.
Neal Stublen Populating a Database  SQLExpress should be installed with Visual Studio  The book provides a.sql file for populating.
Databases and Data Access  Introduction to ADO.NET  ADO.NET objects  ADP.NET namespaces  Differences between ADO and ADO.NET.
.NET Data Access and Manipulation ADO.NET. Overview What is ADO.NET? Disconnected vs. connected data access models ADO.NET Architecture ADO.NET Core Objects.
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.
11 Using ADO.NET II Textbook Chapter Getting Started Last class we started a simple example of using ADO.NET operations to access the Addresses.
Objectives In this lesson, you will learn to: *Identify the need for ADO.NET *Identify the features of ADO.NET *Identify the components of the ADO.NET.
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.
ADO.NET Data Access. Page  2 SQL  When we interact with the datasource through ADO.NET we use the SQL language to retrieve,modify,update information.
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.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
1 Data Classes- DataView DataGridView Control. Objectives 2  Data Class  DataAdapter  DataReader  DataSet  DataTable  DataView  DataGridView Control.
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.
Chapter 24 I’m Suffering from Information Overload (Access Databases) Clearly Visual Basic: Programming with Visual Basic nd Edition.
HNDIT Rapid Application Development
Windows Forms in Visual Studio 2005: An in-depth look at key features Name: Joe Stegman Title: Lead Program Manager Session code.
ADO.NET FUNDAMENTALS BEGINNING ASP.NET 3.5 IN C#.
Coding ADO.NET Objects: Connection, Command, DataReader.
ADO.NET in VB.NET 2005 ITE 370 4/26/2017.
1 Database Programming with ADO.NET Kashef Mughal.
C# .NET Software Development
1 CS 3870/CS 5870: Note07 Prog 4. Master Pages Creating a master page based on another master page MainMasterPage –For all Progs and Tests Prog4MasterPage.
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.
Module 5 Data Classes DataView – DataGridView Control 1.
Coding ADO.Net DataSet Objects ISYS 512. DataSet Object A DataSet object can hold several tables and relationships between tables. A DataSet is a set.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.
.NET Data Access and Manipulation
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
Introduction to Database Processing with ADO.NET
Database, SQL and ADO.NET
Introduction to Database Processing with ADO.NET
Visual Basic 2010 How to Program
ADO.NET and Stored Procedures
Distributed Database Systems
Lecture 6 VB.Net SQL Server.
DataSet и SqlDataAdapter
VB.NET Using Database.
برنامه نویسی سیستم های شی گرا
Lecture Set 10 Windows Controls and Forms
Using List Controls with SQL Server
Database Applications
Chapter 10 Accessing Database Files
ASP.NET and ADO.NET.
M S COLLEGE OF ART’S, COMM., SCI. & BMS Advance Web Programming
Windows Forms in Visual Studio 2005: An in-depth look at key features
Presentation transcript:

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 solution for displaying tabular data with Windows Forms. The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. The DataGridView control makes it easy to define the basic appearance of cells and the display formatting of cell values. The cell is the fundamental unit of interaction for the DataGridView. All cells derive from the DataGridViewCell base class. Each cell within the DataGridView control can have its own style, such as text format, background color, foreground color, and font. Typically, however, multiple cells will share particular style characteristics. The data type for the cell's Value property by default is of type Object.

The DataGridView can display data in Bound mode, unbound mode and Virtual mode.  Bound mode is suitable for managing data using automatic interaction with the data store. One very common use of the DataGridView control is binding to a table in a database.  Unbound mode is suitable for displaying relatively small amounts of data that you manage programmatically.  Virtual mode gives you a higher degree of control by allowing you to wait until a cell is actually being displayed to provide the value it will contain. C# DataGridView Binding - SQL Server dataset

using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"; string sql = "SELECT * FROM Authors"; SqlConnection connection = new SqlConnection(connectionString); SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection); DataSet ds = new DataSet(); connection.Open(); dataadapter.Fill(ds, "Authors"); connection.Close(); dataGridView1.DataSource = ds; dataGridView1.DataMember = "Authors_table); } C# DataGridView Binding - SQL Server dataset

The easiest way to get started using the DataGridView control is to use it in basic data-binding scenarios. When you specify a data source for the DataGridView, by default it will construct columns for you automatically. This will be created based on the data types in the data source. DataGridView Binding - OLEDB dataset

using System; using System.Data; using System.Windows.Forms; using System.Data.OleDb; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=your.mdb file;"; string sql = "SELECT * FROM Authors_table"; OleDbConnection connection = new OleDbConnection(connetionString); OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection); DataSet ds = new DataSet(); connection.Open(); dataadapter.Fill(ds, "Authors_table"); connection.Close(); dataGridView1.DataSource = ds; dataGridView1.DataMember = "Authors_table"; } }} DataGridView Binding - OLEDB dataset

C# DataGridView Add Columns and Rows The DataGridView control is designed to be a complete solution for displaying tabular data with Windows Forms. The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. All cells in a DataGridView control derive from the DataGridViewCell base class. You can access the DataGridView control's columns by using the Columns collection and DataGridView control's rows by using the Rows collection. The below code shows how to manually create Columns and Rows in a DataGridView. dataGridView1.Columns[Index].Name = "Column Name";

C# DataGridView Add Columns and Rows using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { dataGridView1.ColumnCount = 3; dataGridView1.Columns[0].Name = "Product ID"; dataGridView1.Columns[1].Name = "Product Name"; dataGridView1.Columns[2].Name = "Product Price"; string[] row = new string[] { "1", "Product 1", "1000" }; dataGridView1.Rows.Add(row); row = new string[] { "2", "Product 2", "2000" }; dataGridView1.Rows.Add(row); row = new string[] { "3", "Product 3", "3000" }; dataGridView1.Rows.Add(row); row = new string[] { "4", "Product 4", "4000" }; dataGridView1.Rows.Add(row); }

C# DataGridView Sorting and Filtering The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. A DataView provides a means to filter and sort data within a DataTable. The below code shows how to filter and sort a DataGridView by using a DataView. dv = new DataView(ds.Tables[0], "Price > 19", "Price Desc", DataViewRowState.CurrentRows); dataGridView1.DataSource = dv;

C# DataGridView Sorting and Filtering using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"; string sql = "SELECT * FROM Titles"; SqlConnection connection = new SqlConnection(connectionString); SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection); DataSet ds = new DataSet(); connection.Open(); dataadapter.Fill(ds, "Titles_table"); connection.Close(); DataView dv; dv = new DataView(ds.Tables[0], "Price > 19", "Price Desc", DataViewRowState.CurrentRows); dataGridView1.DataSource = dv; } }}

Add Button to C# DataGridView The DataGridView control and its related classes are designed to be a flexible, extensible system for displaying and editing tabular data.The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. The DataGridView control provides TextBox, CheckBox, Image, Button, ComboBox and Link columns with the corresponding cell types. Program on next slide shows how to add a Button in Cell of a DataGridView control. Also it showing in the dataGridView1_CellClick event which button the user clicked.

Add Button to C# DataGridView using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { dataGridView1.ColumnCount = 3; dataGridView1.Columns[0].Name = "Product ID"; dataGridView1.Columns[1].Name = "Product Name"; dataGridView1.Columns[2].Name = "Product Price"; string[] row = new string[] { "1", "Product 1", "1000" }; dataGridView1.Rows.Add(row); row = new string[] { "2", "Product 2", "2000" }; dataGridView1.Rows.Add(row); row = new string[] { "3", "Product 3", "3000" }; dataGridView1.Rows.Add(row); row = new string[] { "4", "Product 4", "4000" }; dataGridView1.Rows.Add(row); DataGridViewButtonColumn btn = new DataGridViewButtonColumn(); dataGridView1.Columns.Add(btn); btn.HeaderText = "Click Data"; btn.Text = "Click Here"; btn.Name = "btn"; btn.UseColumnTextForButtonValue = true; } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 3) { MessageBox.Show((e.RowIndex+1) + " Row " + (e.ColumnIndex+1) + " Column button clicked "); }

C# DataGridView Database Operations using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { SqlCommand sCommand; SqlDataAdapter sAdapter; SqlCommandBuilder sBuilder; DataSet sDs; DataTable sTable; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"; string sql = "SELECT * FROM Stores"; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); sCommand = new SqlCommand(sql, connection); sAdapter = new SqlDataAdapter(sCommand); sBuilder = new SqlCommandBuilder(sAdapter); sDs = new DataSet(); sAdapter.Fill(sDs, "Stores"); sTable = sDs.Tables["Stores"]; connection.Close(); dataGridView1.DataSource = sDs.Tables["Stores"]; dataGridView1.ReadOnly = true; save_btn.Enabled = false; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; } private void new_btn_Click(object sender, EventArgs e) { dataGridView1.ReadOnly = false; save_btn.Enabled = true; new_btn.Enabled = false; delete_btn.Enabled = false; } private void delete_btn_Click(object sender, EventArgs e) { if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes) { dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index); sAdapter.Update(sTable); } private void save_btn_Click(object sender, EventArgs e) { sAdapter.Update(sTable); dataGridView1.ReadOnly = true; save_btn.Enabled = false; new_btn.Enabled = true; delete_btn.Enabled = true; }

VScrollBar v = new VScrollBar(); v.Height = 300; v.Width = 23; v.Maximum = 100; v.Minimum = 20; v.Value = 45; this.Controls.Add(v); v.Scroll += new ScrollEventHandler(v_Scroll); private void v_Scroll(object sender, ScrollEventArgs e) { Graphics g = pictureBox1.CreateGraphics(); g.DrawImage(pictureBox1.Image, new Rectangle(0, 0, pictureBox1.Height, v.Value)); } Usage of VScrollbar

int _selectedIndex; string _text; private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { // Called when a new index is selected. _selectedIndex = comboBox1.SelectedIndex; _text = comboBox1.Text; Display(); } void Display() { this.Text = string.Format("Text: {0}; SelectedIndex: {1}", _text, _selectedIndex); } Usage of Combobox

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { comboBox2.Items.Clear(); if (comboBox1.SelectedItem.ToString() == "1") { comboBox2.Items.Add("Hello"); comboBox2.Items.Add("World"); } else { comboBox2.Items.Add("Test1"); comboBox2.Items.Add("Test2"); } comboBox1.items.clear Usage of Combobox to add item

private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); using (SqlConnection Cn = new SqlConnection(Properties.Settings.Default.con)) { using (SqlCommand Cmd = new SqlCommand("SELECT * FROM Orders where oid Cn)) { Cn.Open(); SqlDataAdapter adpt = new SqlDataAdapter(Cmd); adpt.Fill(dt); comboBox1.DataSource = dt; comboBox1.DisplayMember="Orders"; comboBox1.ValueMember="id"; } Usage of Combobox with database

dataset ds = new dataset() ; string querry = "select studentid,studentname from tbstudent"; sqlcommand cmd = new sqlcommand(querry,connection); dataadapter da = new sqldataadapter(cmd); combobox1.valuemember = "studentid"; combobox1.displaymember = "studentname"; combobox1.datasource = ds.tables(0); Another example: string movies = ""; if (checkBox1.Checked) { movies = movies + checkBox1.Text; } MessageBox.Show(movies); or string ChosenMovie = ""; if (radioButton1.Checked) { ChosenMovie = radioButton1.Text; } else if (radioButton2.Checked) { ChosenMovie = radioButton2.Text; }

RichTextBox richTextBox1.SaveFile(“F:\\samples\\ …”); richTextbox1.LoadFile(“ ”) richTextBox1.SelectionColor=Color.Green;