SlideShare a Scribd company logo
1 of 33
1
A Programme Under the Compumitra Series
GridView Control–CS
LAB WORK GUIDE
Data binding is a functionality that allows us to connect data
from a source to a target.
Data Binding in GridView using wizard
 Example Template Creation.
 Database file addition in App_data
 Data Binding with GridView using wizard
 Output
 Code explanation
 Modification Trials
Data Binding in GridView using CS Code
 Example Template Creation.
 Data Binding with GridView using vb code
 Output
 Code explanation
 Modification Trials
 Error Trials
 Practice Exercise
 Learning Summary Review
 References.
OUTLINE
Data Binding
Using GridView control through wizard
Data binding is a general technique that binds
two data/information sources together and
maintains them in sync.
The GridView control enables you to connect to a
data source and display data in tabular format.
GridViewUsingWizardCS: Creating And Renaming Webpage
2. Now Rename this page with "Rename"
option by Right Clicking on "Default.aspx"
1. Select the "Default.aspx" page
in the "Solution Explorer"
3. Rename "Default.aspx"
page to "GridView.aspx" page.
• Follow Standard Website Creation Steps and set your path to
C:Learner<student-id>DataBindingGridViewUsingWizardCS
• Add New Default.aspx page in your Website
1. Select the "Root Path" in the
"Solution Explorer" and Right
Click on it
2. Now select "Add ASP.NET
Folder" option
3. And then select "App_Data"
GridViewUsingWizardCS: Adding App_Data Folder
Adding Database To App_Data
GridViewUsingWizardCS: Adding Database-1
Right Click on "App_Data" and select "Add
Existing Item.." option in Solution Explorer
to attach the database to "App_Data"
For attaching database to your current website follow the
instruction.
GridViewUsingWizardCS: Adding Database-2
1. Select the database you
created
2. Attach the database by
clicking the ‘Add’ Button
Your attached database will
appear in 'App_Data' folder.
To attach the database follow the path "C:Learner<Student-
Id>DatabaseSale".
GridViewUsingWizardCS: Adding Gridview Control
The GridView control enables you to connect to a data source and
display data in tabular format
Drag and Drop "GridView" control
in div From 'Data' Toolbox
Select '<New data source…>' option
from 'Choose Data Source:'
dropdown list of 'GridView Tasks' to
attach a data source in GridView
control.
GridViewUsingWizardCS: Adding AccessDataSource in GridView-1
1. Select "Access
Database" and then
click "OK" button.
2. Click on "Browse" button to
browse for database file
1. Double click on App_Data and select
the database and click "OK" button
Now follow the instructions to select the database
GridViewUsingWizardCS: Adding AccessDataSource in GridView-2
2. Click "Next"
GridViewUsingWizardCS: Adding AccessDataSource in GridView-3
1. Select the table which you
want to access from the
database from DropDown list
If you have applied any query on the tables you can also select
that query from the DropDownList.
Select the fields which You
want to display and click
"Next" button
GridViewUsingWizardCS: Adding AccessDataSource in GridView-4
1. First Click "Test Query" to
test the query
2. Then Click "Finish" button
Test Query is used to test that whether the data is accessed
according to our need or not.
Your tested query will
show here.
GridViewUsingWizardCS: View Of AccessDataSource
'AccessDataSource' control
will automatically appear
here.
Now Run Code By
pressing 'F5'
GridViewUsingWizardCS: Output
Output on browser
Content of the ItemMaster table
will display in GridView
GridViewUsingWizardCS: Source Code Explanation -1
"AutoGenerateColumn" is
used to generate bound
fields automatically.
This source code describes your attached database
and select query
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ItemId" DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="ItemId" HeaderText="ItemId" InsertVisible="False"
ReadOnly="True" SortExpression="ItemId" />
<asp:BoundField DataField="Item_name" HeaderText="Item_name"
SortExpression="Item_name" />
<asp:BoundField DataField="unit" HeaderText="unit" SortExpression="unit" />
<asp:BoundField DataField="Product_Rate" HeaderText="Product_Rate"
SortExpression="Product_Rate" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/sale.mdb" SelectCommand="SELECT * FROM [ItemMaster]">
</asp:AccessDataSource>
"DataKeyNames" defines
Primary key in the table
"DataSourceID" defines
data source as
"AccessdataSource1"
GridViewUsingWizardCS: Source Code Explanation -2
"DataField" Displays name
of the field
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ItemId" DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="ItemId" HeaderText="ItemId" InsertVisible="False"
ReadOnly="True" SortExpression="ItemId" />
<asp:BoundField DataField="Item_name" HeaderText="Item_name"
SortExpression="Item_name" />
<asp:BoundField DataField="unit" HeaderText="unit" SortExpression="unit" />
<asp:BoundField DataField="Product_Rate" HeaderText="Product_Rate"
SortExpression="Product_Rate" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/sale.mdb" SelectCommand="SELECT * FROM [ItemMaster]">
</asp:AccessDataSource>
"BoundField" is to define
different fields in the
GridView.
"SortExpression" Defines
name of the field on which
sorting can be performed
"HeaderText" shows Header
name of the column
GridViewUsingWizardCS: Modification trials
 Select "All Fields" query instead of "ItemMaster" and observe
the difference.
You will see that you are getting the data from all the
tables.
 Instead of selecting ‘*’ while selecting columns select any three
column and see the difference.
Now you can observe from the result that only selected fields
are showing.
 Change the Header Text of column "Item_name" from
"Item_name" to "Product Name" and see the difference after
running the page.
You will see that Header of column is changed.
Data Binding
Using GridView control through CS Code
Data binding provides a simple and consistent
way for applications to present and interact
with data.
The GridView control enables you to connect to
a data source and display data in tabular
format.
GridViewUsingCSCode: .aspx File Creation
1. Drag and Drop a 'Button' control
in div from Standard Toolbox
• Follow Standard Website Creation Steps and set your path to
"C:Learner<student-id>DataBindingGridViewUsingCSCode"
• Add New Default.aspx page in your Website
2. Introduce a new line and Drag and Drop
a 'GridView' control from Data Toolbox
•Add the 'App_Data' folder
•To add the database, follow the path "C:Learner<Student-
Id>DatabaseSale".
GridViewUsingCSCode: Adding Database
1. Add 'App_Data' folder
2. And Database 'Sale.mdb' in
App_Data like previous exercise.
GridViewUsingCSCode: Copy/Paste Code
using System.Data;
using System.Data.OleDb;
Type this code above "Partial
Class _Default"
Go to Default.aspx.cs page
GridViewUsingCSCode: Button1_Click Event handler Code
string conString =@"Provider=Microsoft.JET.OLEDB.4.0;"
+ @"data source= " + Server.MapPath("~/App_Data/sale.mdb");
OleDbConnection conn = new OleDbConnection(conString);
conn.Open();
OleDbCommand aCommand = new OleDbCommand("select * from ItemMaster",
conn);
OleDbDataReader aReader = aCommand.ExecuteReader();
GridView1.DataSource = aReader;
GridView1.DataBind();
conn.Close();
• Generate the Button1_Click handler in the CS code by double-clicking
over the button control of aspx file.
Copy/Paste or Type this code in Button1_Click Event
Handler and run the code with "F5"
GridViewUsingCSCode: Run GridView Page
First click the button
Result will show here.
When you will run your page then after clicking button you will get the output
GridViewUsingCSCode: Code Explanation-1
string conString =@"Provider=Microsoft.JET.OLEDB.4.0;"+ @"data
source= "; Server.MapPath("~/App_Data/sale.mdb");
OleDbConnection conn = new OleDbConnection(conString);
conn.Open();
Opening Connection with
‘Open()’ method
This string type argument is passed, when
we create a connection with MS Access
This statement creates an object
of OleDbConnection class.
This is a path of Database.
using System.Data;
using System.Data.OleDb;
Includes classes which lets us handle
data from data sources
Includes classes that provide
OleDb.Net provider
GridViewUsingCSCode: Code Explanation-2
OleDbCommand aCommand = new OleDbCommand("select *
from ItemMaster", conn);
OleDbDataReader aReader = aCommand.ExecuteReader();
GridView1.DataSource = aReader;
GridView1.DataBind();
conn.Close();
Define the DataSource ID of
GridView equal to DataReader
This statement execute the SQL
query and store its result in
OleDbDataReader class object
This statement creates an object
of OleDbConnection class. This
needs two argument, 1- SQL
Query, 2- OleDbConnection class
object
DataBinding in GridView
Closing Connection
GridViewUsingCSCode: Modification Trails-1
 In SELECT statement, replace '*' sign with 'ItemId,
Item_name'
Run and watch the effect
Now you can observe from the result that only
selected fields have been displayed.
 Add 'As Product_Name' just after the 'Item_name'
SELECT statement.
Syntax: <Field Name> As <New Name>
You will see that Header of column will change
when you will run the page.
GridViewUsingCSCode: Modification Trails-2
 Click on 'Auto Format…' link in 'GridView
Task' and select the 'scheme' equal to 'Black
& Blue 2'
Run and watch the effect
GridViewUsingCSCode: Error Trails
 Remove the Namespace files which we have inserted in the
beginning.
You will get the error as given below
BC30002: Type 'OleDbConnection' is not defined.
 Remove the line dbconn.open() and see the result after
running the page.
The error will be shown that is given below.
ExecuteReader requires an open and available
Connection. The connection's current state is closed.
To remove this error connection should be open.
GridViewUsingCSCode: Practice Exercise
Create a website named Expenses
Add the Database 'Monthly Expenses'
Show column "CatId" and "Description"
from table "Expense Category" and "Date"
and "Detail" from table "Expenses" in a
Grid view using wizard option.
Show column "CatId" and "Description"
from the "Expense Category" table and
"Date" and "Detail" from the "Expenses"
table in a Grid view using code.
GridViewUsingCSCode: Learning Summary Review
Use of GridView control
Adding database to App_Data.
Linking of MS Access database with Grid View
through wizard.
Linking of MS Access database with Grid View
through Code.
Use of OleDb classes.
Bibliography
http://msdn.microsoft.com/en-
us/library/fbk67b6z.aspx
http://www.w3Schools.com
http://www.switchonthecode.com/tutorials/csharp-
tutorial-binding-a-datagridview-to-a-database
http://www.codeproject.com/KB/aspnet/DataGridVi
ew__GridView.aspx
http://www.codersource.net/asp-net/asp-net-2-
0/grid-view-control-in-asp-net-2-0.aspx
Ask and guide me at
sunmitraeducation@gmail.com
Share this information with as many people as
possible.
Keep visiting www.sunmitra.com for
programme updates.

More Related Content

What's hot

What's hot (20)

ASP.NET Lecture 4
ASP.NET Lecture 4ASP.NET Lecture 4
ASP.NET Lecture 4
 
Ch 7 data binding
Ch 7 data bindingCh 7 data binding
Ch 7 data binding
 
Data Binding
Data BindingData Binding
Data Binding
 
For Beginers - ADO.Net
For Beginers - ADO.NetFor Beginers - ADO.Net
For Beginers - ADO.Net
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
Disconnected data
Disconnected dataDisconnected data
Disconnected data
 
Ado.net
Ado.netAdo.net
Ado.net
 
ADO.NET -database connection
ADO.NET -database connectionADO.NET -database connection
ADO.NET -database connection
 
ASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And RepresentationASP.NET 08 - Data Binding And Representation
ASP.NET 08 - Data Binding And Representation
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
 
Chapter 3: ado.net
Chapter 3: ado.netChapter 3: ado.net
Chapter 3: ado.net
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
 
Configuring Data Binding part2 ABTO Software Lecture Korotchyn
Configuring Data Binding part2 ABTO Software Lecture KorotchynConfiguring Data Binding part2 ABTO Software Lecture Korotchyn
Configuring Data Binding part2 ABTO Software Lecture Korotchyn
 
ADO CONTROLS - Database usage
ADO CONTROLS - Database usageADO CONTROLS - Database usage
ADO CONTROLS - Database usage
 
Configuring Data Binding part1 ABTO Software Lecture Korotchyn
Configuring Data Binding part1 ABTO Software Lecture KorotchynConfiguring Data Binding part1 ABTO Software Lecture Korotchyn
Configuring Data Binding part1 ABTO Software Lecture Korotchyn
 
ADO.NET by ASP.NET Development Company in india
ADO.NET by ASP.NET  Development Company in indiaADO.NET by ASP.NET  Development Company in india
ADO.NET by ASP.NET Development Company in india
 
Ado.Net Tutorial
Ado.Net TutorialAdo.Net Tutorial
Ado.Net Tutorial
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Mvc acchitecture
Mvc acchitectureMvc acchitecture
Mvc acchitecture
 
Ado.net
Ado.netAdo.net
Ado.net
 

Similar to Grid View Control CS

Open microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletOpen microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletMitchinson
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studioAravindharamanan S
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studioAravindharamanan S
 
ASP.NET Session 13 14
ASP.NET Session 13 14ASP.NET Session 13 14
ASP.NET Session 13 14Sisir Ghosh
 
Cis407 a ilab 3 web application development devry university
Cis407 a ilab 3 web application development devry universityCis407 a ilab 3 web application development devry university
Cis407 a ilab 3 web application development devry universitylhkslkdh89009
 
DATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEMDATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEMVj NiroSh
 
07 asp.net session10
07 asp.net session1007 asp.net session10
07 asp.net session10Mani Chaubey
 
IntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdfIntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdfDavid Harrison
 
IntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdfIntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdfDavid Harrison
 
07 asp.net session10
07 asp.net session1007 asp.net session10
07 asp.net session10Vivek chan
 
Mvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaMvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaalifha12
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08Mani Chaubey
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08Vivek chan
 
VISUAL BASIC .net data accesss vii
VISUAL BASIC .net data accesss viiVISUAL BASIC .net data accesss vii
VISUAL BASIC .net data accesss viiargusacademy
 

Similar to Grid View Control CS (20)

Grid view control
Grid view controlGrid view control
Grid view control
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
data binding.docx
data binding.docxdata binding.docx
data binding.docx
 
Open microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutletOpen microsoft visual studio/tutorialoutlet
Open microsoft visual studio/tutorialoutlet
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studio
 
Simple ado program by visual studio
Simple ado program by visual studioSimple ado program by visual studio
Simple ado program by visual studio
 
ASP.NET Session 13 14
ASP.NET Session 13 14ASP.NET Session 13 14
ASP.NET Session 13 14
 
Cis407 a ilab 3 web application development devry university
Cis407 a ilab 3 web application development devry universityCis407 a ilab 3 web application development devry university
Cis407 a ilab 3 web application development devry university
 
Android sql examples
Android sql examplesAndroid sql examples
Android sql examples
 
DATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEMDATABASE MANAGEMENT SYSTEM
DATABASE MANAGEMENT SYSTEM
 
07 asp.net session10
07 asp.net session1007 asp.net session10
07 asp.net session10
 
IntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdfIntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdf
 
IntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdfIntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdf
 
Chapter12 (1)
Chapter12 (1)Chapter12 (1)
Chapter12 (1)
 
07 asp.net session10
07 asp.net session1007 asp.net session10
07 asp.net session10
 
Mvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senjaMvc4 crud operations.-kemuning senja
Mvc4 crud operations.-kemuning senja
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08
 
VISUAL BASIC .net data accesss vii
VISUAL BASIC .net data accesss viiVISUAL BASIC .net data accesss vii
VISUAL BASIC .net data accesss vii
 
Ado Presentation
Ado PresentationAdo Presentation
Ado Presentation
 

More from sunmitraeducation

More from sunmitraeducation (20)

Java Introduction
Java IntroductionJava Introduction
Java Introduction
 
Installing JDK and first java program
Installing JDK and first java programInstalling JDK and first java program
Installing JDK and first java program
 
Project1 VB
Project1 VBProject1 VB
Project1 VB
 
Project1 CS
Project1 CSProject1 CS
Project1 CS
 
Ms Access
Ms AccessMs Access
Ms Access
 
Database Basics Theory
Database Basics TheoryDatabase Basics Theory
Database Basics Theory
 
Visual Web Developer and Web Controls VB set 3
Visual Web Developer and Web Controls VB set 3Visual Web Developer and Web Controls VB set 3
Visual Web Developer and Web Controls VB set 3
 
Visual Web Developer and Web Controls CS set 3
Visual Web Developer and Web Controls CS set 3Visual Web Developer and Web Controls CS set 3
Visual Web Developer and Web Controls CS set 3
 
Progamming Primer Polymorphism (Method Overloading) VB
Progamming Primer Polymorphism (Method Overloading) VBProgamming Primer Polymorphism (Method Overloading) VB
Progamming Primer Polymorphism (Method Overloading) VB
 
Programming Primer EncapsulationVB
Programming Primer EncapsulationVBProgramming Primer EncapsulationVB
Programming Primer EncapsulationVB
 
Programming Primer Encapsulation CS
Programming Primer Encapsulation CSProgramming Primer Encapsulation CS
Programming Primer Encapsulation CS
 
Programming Primer Inheritance VB
Programming Primer Inheritance VBProgramming Primer Inheritance VB
Programming Primer Inheritance VB
 
Programming Primer Inheritance CS
Programming Primer Inheritance CSProgramming Primer Inheritance CS
Programming Primer Inheritance CS
 
ProgrammingPrimerAndOOPS
ProgrammingPrimerAndOOPSProgrammingPrimerAndOOPS
ProgrammingPrimerAndOOPS
 
Web Server Controls VB Set 1
Web Server Controls VB Set 1Web Server Controls VB Set 1
Web Server Controls VB Set 1
 
Web Server Controls CS Set
Web Server Controls CS Set Web Server Controls CS Set
Web Server Controls CS Set
 
Web Controls Set-1
Web Controls Set-1Web Controls Set-1
Web Controls Set-1
 
Understanding IDEs
Understanding IDEsUnderstanding IDEs
Understanding IDEs
 
Html Server Image Control VB
Html Server Image Control VBHtml Server Image Control VB
Html Server Image Control VB
 
Html Server Image Control CS
Html Server Image Control CSHtml Server Image Control CS
Html Server Image Control CS
 

Recently uploaded

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

Grid View Control CS

  • 1. 1 A Programme Under the Compumitra Series GridView Control–CS LAB WORK GUIDE Data binding is a functionality that allows us to connect data from a source to a target.
  • 2. Data Binding in GridView using wizard  Example Template Creation.  Database file addition in App_data  Data Binding with GridView using wizard  Output  Code explanation  Modification Trials Data Binding in GridView using CS Code  Example Template Creation.  Data Binding with GridView using vb code  Output  Code explanation  Modification Trials  Error Trials  Practice Exercise  Learning Summary Review  References. OUTLINE
  • 3. Data Binding Using GridView control through wizard Data binding is a general technique that binds two data/information sources together and maintains them in sync. The GridView control enables you to connect to a data source and display data in tabular format.
  • 4. GridViewUsingWizardCS: Creating And Renaming Webpage 2. Now Rename this page with "Rename" option by Right Clicking on "Default.aspx" 1. Select the "Default.aspx" page in the "Solution Explorer" 3. Rename "Default.aspx" page to "GridView.aspx" page. • Follow Standard Website Creation Steps and set your path to C:Learner<student-id>DataBindingGridViewUsingWizardCS • Add New Default.aspx page in your Website
  • 5. 1. Select the "Root Path" in the "Solution Explorer" and Right Click on it 2. Now select "Add ASP.NET Folder" option 3. And then select "App_Data" GridViewUsingWizardCS: Adding App_Data Folder
  • 7. GridViewUsingWizardCS: Adding Database-1 Right Click on "App_Data" and select "Add Existing Item.." option in Solution Explorer to attach the database to "App_Data" For attaching database to your current website follow the instruction.
  • 8. GridViewUsingWizardCS: Adding Database-2 1. Select the database you created 2. Attach the database by clicking the ‘Add’ Button Your attached database will appear in 'App_Data' folder. To attach the database follow the path "C:Learner<Student- Id>DatabaseSale".
  • 9. GridViewUsingWizardCS: Adding Gridview Control The GridView control enables you to connect to a data source and display data in tabular format Drag and Drop "GridView" control in div From 'Data' Toolbox Select '<New data source…>' option from 'Choose Data Source:' dropdown list of 'GridView Tasks' to attach a data source in GridView control.
  • 10. GridViewUsingWizardCS: Adding AccessDataSource in GridView-1 1. Select "Access Database" and then click "OK" button. 2. Click on "Browse" button to browse for database file
  • 11. 1. Double click on App_Data and select the database and click "OK" button Now follow the instructions to select the database GridViewUsingWizardCS: Adding AccessDataSource in GridView-2 2. Click "Next"
  • 12. GridViewUsingWizardCS: Adding AccessDataSource in GridView-3 1. Select the table which you want to access from the database from DropDown list If you have applied any query on the tables you can also select that query from the DropDownList. Select the fields which You want to display and click "Next" button
  • 13. GridViewUsingWizardCS: Adding AccessDataSource in GridView-4 1. First Click "Test Query" to test the query 2. Then Click "Finish" button Test Query is used to test that whether the data is accessed according to our need or not. Your tested query will show here.
  • 14. GridViewUsingWizardCS: View Of AccessDataSource 'AccessDataSource' control will automatically appear here. Now Run Code By pressing 'F5'
  • 15. GridViewUsingWizardCS: Output Output on browser Content of the ItemMaster table will display in GridView
  • 16. GridViewUsingWizardCS: Source Code Explanation -1 "AutoGenerateColumn" is used to generate bound fields automatically. This source code describes your attached database and select query <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ItemId" DataSourceID="AccessDataSource1"> <Columns> <asp:BoundField DataField="ItemId" HeaderText="ItemId" InsertVisible="False" ReadOnly="True" SortExpression="ItemId" /> <asp:BoundField DataField="Item_name" HeaderText="Item_name" SortExpression="Item_name" /> <asp:BoundField DataField="unit" HeaderText="unit" SortExpression="unit" /> <asp:BoundField DataField="Product_Rate" HeaderText="Product_Rate" SortExpression="Product_Rate" /> </Columns> </asp:GridView> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/sale.mdb" SelectCommand="SELECT * FROM [ItemMaster]"> </asp:AccessDataSource> "DataKeyNames" defines Primary key in the table "DataSourceID" defines data source as "AccessdataSource1"
  • 17. GridViewUsingWizardCS: Source Code Explanation -2 "DataField" Displays name of the field <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ItemId" DataSourceID="AccessDataSource1"> <Columns> <asp:BoundField DataField="ItemId" HeaderText="ItemId" InsertVisible="False" ReadOnly="True" SortExpression="ItemId" /> <asp:BoundField DataField="Item_name" HeaderText="Item_name" SortExpression="Item_name" /> <asp:BoundField DataField="unit" HeaderText="unit" SortExpression="unit" /> <asp:BoundField DataField="Product_Rate" HeaderText="Product_Rate" SortExpression="Product_Rate" /> </Columns> </asp:GridView> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/sale.mdb" SelectCommand="SELECT * FROM [ItemMaster]"> </asp:AccessDataSource> "BoundField" is to define different fields in the GridView. "SortExpression" Defines name of the field on which sorting can be performed "HeaderText" shows Header name of the column
  • 18. GridViewUsingWizardCS: Modification trials  Select "All Fields" query instead of "ItemMaster" and observe the difference. You will see that you are getting the data from all the tables.  Instead of selecting ‘*’ while selecting columns select any three column and see the difference. Now you can observe from the result that only selected fields are showing.  Change the Header Text of column "Item_name" from "Item_name" to "Product Name" and see the difference after running the page. You will see that Header of column is changed.
  • 19. Data Binding Using GridView control through CS Code Data binding provides a simple and consistent way for applications to present and interact with data. The GridView control enables you to connect to a data source and display data in tabular format.
  • 20. GridViewUsingCSCode: .aspx File Creation 1. Drag and Drop a 'Button' control in div from Standard Toolbox • Follow Standard Website Creation Steps and set your path to "C:Learner<student-id>DataBindingGridViewUsingCSCode" • Add New Default.aspx page in your Website 2. Introduce a new line and Drag and Drop a 'GridView' control from Data Toolbox
  • 21. •Add the 'App_Data' folder •To add the database, follow the path "C:Learner<Student- Id>DatabaseSale". GridViewUsingCSCode: Adding Database 1. Add 'App_Data' folder 2. And Database 'Sale.mdb' in App_Data like previous exercise.
  • 22. GridViewUsingCSCode: Copy/Paste Code using System.Data; using System.Data.OleDb; Type this code above "Partial Class _Default" Go to Default.aspx.cs page
  • 23. GridViewUsingCSCode: Button1_Click Event handler Code string conString =@"Provider=Microsoft.JET.OLEDB.4.0;" + @"data source= " + Server.MapPath("~/App_Data/sale.mdb"); OleDbConnection conn = new OleDbConnection(conString); conn.Open(); OleDbCommand aCommand = new OleDbCommand("select * from ItemMaster", conn); OleDbDataReader aReader = aCommand.ExecuteReader(); GridView1.DataSource = aReader; GridView1.DataBind(); conn.Close(); • Generate the Button1_Click handler in the CS code by double-clicking over the button control of aspx file. Copy/Paste or Type this code in Button1_Click Event Handler and run the code with "F5"
  • 24. GridViewUsingCSCode: Run GridView Page First click the button Result will show here. When you will run your page then after clicking button you will get the output
  • 25. GridViewUsingCSCode: Code Explanation-1 string conString =@"Provider=Microsoft.JET.OLEDB.4.0;"+ @"data source= "; Server.MapPath("~/App_Data/sale.mdb"); OleDbConnection conn = new OleDbConnection(conString); conn.Open(); Opening Connection with ‘Open()’ method This string type argument is passed, when we create a connection with MS Access This statement creates an object of OleDbConnection class. This is a path of Database. using System.Data; using System.Data.OleDb; Includes classes which lets us handle data from data sources Includes classes that provide OleDb.Net provider
  • 26. GridViewUsingCSCode: Code Explanation-2 OleDbCommand aCommand = new OleDbCommand("select * from ItemMaster", conn); OleDbDataReader aReader = aCommand.ExecuteReader(); GridView1.DataSource = aReader; GridView1.DataBind(); conn.Close(); Define the DataSource ID of GridView equal to DataReader This statement execute the SQL query and store its result in OleDbDataReader class object This statement creates an object of OleDbConnection class. This needs two argument, 1- SQL Query, 2- OleDbConnection class object DataBinding in GridView Closing Connection
  • 27. GridViewUsingCSCode: Modification Trails-1  In SELECT statement, replace '*' sign with 'ItemId, Item_name' Run and watch the effect Now you can observe from the result that only selected fields have been displayed.  Add 'As Product_Name' just after the 'Item_name' SELECT statement. Syntax: <Field Name> As <New Name> You will see that Header of column will change when you will run the page.
  • 28. GridViewUsingCSCode: Modification Trails-2  Click on 'Auto Format…' link in 'GridView Task' and select the 'scheme' equal to 'Black & Blue 2' Run and watch the effect
  • 29. GridViewUsingCSCode: Error Trails  Remove the Namespace files which we have inserted in the beginning. You will get the error as given below BC30002: Type 'OleDbConnection' is not defined.  Remove the line dbconn.open() and see the result after running the page. The error will be shown that is given below. ExecuteReader requires an open and available Connection. The connection's current state is closed. To remove this error connection should be open.
  • 30. GridViewUsingCSCode: Practice Exercise Create a website named Expenses Add the Database 'Monthly Expenses' Show column "CatId" and "Description" from table "Expense Category" and "Date" and "Detail" from table "Expenses" in a Grid view using wizard option. Show column "CatId" and "Description" from the "Expense Category" table and "Date" and "Detail" from the "Expenses" table in a Grid view using code.
  • 31. GridViewUsingCSCode: Learning Summary Review Use of GridView control Adding database to App_Data. Linking of MS Access database with Grid View through wizard. Linking of MS Access database with Grid View through Code. Use of OleDb classes.
  • 33. Ask and guide me at sunmitraeducation@gmail.com Share this information with as many people as possible. Keep visiting www.sunmitra.com for programme updates.