SlideShare una empresa de Scribd logo
1 de 20
In this chapter, you’ll concentrate on three
more advanced controls that allow you to
bind entire tables of data.

• GridView
• Details View
• FormView
GridView: The GridView is an all-purpose grid control for
showing large tables of information. it comes equipped
with the most ready-made functionality like sorting,
paging, selecting, editing and deleting.

DetailsView: The DetailsView is ideal for showing a
single record at a time. The DetailsView also supports
editing.

FormView: Like the DetailsView, the FormView shows a
single record at a time and supports editing. The
difference is that the FormView is based on templates,
which allow you to combine fields in a flexible layout that
doesn’t need to be table-based.
•
protected void Page_Load(object sender, EventArgs e)
{
string connectionString =
WebConfigurationManager.ConnectionStrings["Northwind"].Connectio
nString;

string selectSQL = "SELECT ProductID, ProductName, UnitPrice
FROM Products";

SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);


DataSet ds = new DataSet();
adapter.Fill(ds, "Products");

GridView1.DataSource = ds;
GridView1.DataBind();
}
Of course, you don’t need to write this data access code by hand.

Define a SqlDataSource to perform the query shown in the previous
example:

<asp:SqlDataSource ID="sourceProducts" runat="server"
ConnectionString="<%$ ConnectionStrings:Northwind %>"
SelectCommand="SELECT ProductID, ProductName, UnitPrice
FROM Products" />

Next, set the GridView.DataSourceID property to link the data source
to your grid:

<asp:GridView ID="GridView1" runat="server"
DataSourceID="sourceProducts" />
By default, the GridView.AutoGenerateColumns property
is true, and the GridView creates a column for each field
in the bound DataTable.

This automatic column generation is good for creating
quick test pages, but it doesn’t give you the flexibility you’ll
usually want. For example, what if you want to hide
columns, change their order, or configure some aspect of
their display, such as the formatting or heading text?

In all these cases, you need to set AutoGenerateColumns
to false and define the columns in the <Columns> section
of the GridView control tag.
Now that you understand the underpinnings of the GridView,
you’ve still only started to explore its higher-level features.

Formatting: How to format rows and data values
Selecting: How to let users select a row in the GridView and
respond accordingly
Editing: How to let users commit record updates, inserts,
and deletes
Sorting: How to dynamically reorder the GridView in
response to clicks on a column header
Paging: How to divide a large result set into multiple pages
of data
Templates: How to take complete control of designing,
formatting, and editing by defining templates
You handle this job with the DataFormatString property.

Each BoundField column provides a DataFormatString
property you can use to configure the appearance of
numbers and dates using a format string.

<asp:BoundField DataField="UnitPrice"
HeaderText="Price"
DataFormatString="{0:C}" />

<asp:BoundField DataField="BirthDate"
HeaderText="Birth Date"
DataFormatString="{0:MM/dd/yy}" />
protected void GridView1_RowDataBound(object
sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the price for this row.
decimal price =
(decimal)DataBinder.Eval(e.Row.DataItem, "UnitPrice");
if (price > 50)
{
e.Row.BackColor = System.Drawing.Color.Maroon;
e.Row.ForeColor = System.Drawing.Color.White;
e.Row.Font.Bold = true;
}
}
}
The GridView provides built-in support for selection. You
simply need to add a CommandField column with the
ShowSelectButton property set to true.

ASP.NET can render the CommandField as a hyperlink, a
button, or a fixed image.

<asp:CommandField ShowSelectButton="True"
ButtonType="Button“ SelectText="Select" />

<asp:CommandField ShowSelectButton="True"
ButtonType="Image“ SelectImageUrl="select.gif" />
You don’t need to create a new column to support
row selection. Instead, you can turn an existing
column into a link.

<asp:ButtonField CommandName="Select"
ButtonType="Button"
DataTextField="ProductID" />
A typical master-details page has two GridView controls.
The first GridView shows the master (or parent) table.

When a user selects an item in the first GridView, the
second GridView is filled with related records from the
details (or parent) table.
Use the CommandField column, set ShowEditButton to true.




To enable sorting, you must set the GridView.AllowSorting
property to true. Next, you need to define a SortExpression
for each column that can be sorted.

To use automatic paging, you need to set AllowPaging to
true (which shows the page controls), and you need to set
PageSize to determine how many rows are allowed on
each page.
You can use each SqlDataSource control you create,
to retrieve a single query.

The SqlDataSource command logic is supplied through
four properties—SelectCommand, InsertCommand,
UpdateCommand, and DeleteCommand—each of which
takes a string.

<asp:SqlDataSource ID="sourceProducts" runat="server"
ConnectionString="<%$ ConnectionStrings:Northwind %>"
SelectCommand="SELECT ProductName, ProductID
FROM Products“ />
<asp:SqlDataSource ID="sourceProductDetails" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:Northwind %>“

SelectCommand="SELECT * FROM Products WHERE
ProductID=@ProductID"/>

<asp:SqlDataSource ID="sourceProductDetails" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:Northwind %>"
SelectCommand="SELECT * FROM Products WHERE
ProductID=@ProductID">

<SelectParameters>
<asp:ControlParameter ControlID="lstProduct" Name="ProductID"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
If bound to Rich Controls SqlDataSource can display
many fields at a time as compared to List Controls which
display only one field at a time.

Example:

Gridview
DetailsView
For example, you could split the earlier example into two pages. In the first
page, define a list control that shows all the available products:

<asp:SqlDataSource ID="sourceProducts" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:Northwind %>"
SelectCommand="SELECT ProductName, ProductID FROM Products"/>

<asp:DropDownList ID="lstProduct" runat="server" AutoPostBack="True"
DataSourceID="sourceProducts" DataTextField="ProductName"
DataValueField="ProductID" />
protected void cmdGo_Click(object sender, EventArgs e)
{
if (lstProduct.SelectedIndex != -1)
{
Response.Redirect(
"QueryParameter2.aspx?prodID=" + lstProduct.SelectedValue);
}
}
Finally, the second page can bind the DetailsView according to the ProductID value
that’s supplied in the query string:

<asp:SqlDataSource ID="sourceProductDetails" runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="<%$ ConnectionStrings:Northwind %>"
SelectCommand="SELECT * FROM Products WHERE ProductID=@ProductID">
<SelectParameters>
<asp:QueryStringParameter Name="ProductID" QueryStringField="prodID" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="detailsProduct" runat="server"
DataSourceID="sourceProductDetails" />

Más contenido relacionado

Similar a Chapter 16

Step by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView TutorialsStep by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView TutorialsNilesh kumar Jadav
 
Detail view in distributed technologies
Detail view in distributed technologiesDetail view in distributed technologies
Detail view in distributed technologiesjamessakila
 
AdRotator and AdRepeater Control in Asp.Net for Msc CS
AdRotator and AdRepeater Control in Asp.Net for Msc CSAdRotator and AdRepeater Control in Asp.Net for Msc CS
AdRotator and AdRepeater Control in Asp.Net for Msc CSThanveen
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08Vivek chan
 
High performance coding practices code project
High performance coding practices code projectHigh performance coding practices code project
High performance coding practices code projectPruthvi B Patil
 
IntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdfIntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdfDavid Harrison
 
IntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdfIntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdfDavid Harrison
 
Asp Net Advance Topics
Asp Net Advance TopicsAsp Net Advance Topics
Asp Net Advance TopicsAli Taki
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08Mani Chaubey
 
ASP.Net 3.5 SP1 Dynamic Data
ASP.Net 3.5 SP1 Dynamic DataASP.Net 3.5 SP1 Dynamic Data
ASP.Net 3.5 SP1 Dynamic Datamicham
 
MVVM & Data Binding Library
MVVM & Data Binding Library MVVM & Data Binding Library
MVVM & Data Binding Library 10Clouds
 

Similar a Chapter 16 (20)

GRID VIEW PPT
GRID VIEW PPTGRID VIEW PPT
GRID VIEW PPT
 
Grid view control
Grid view controlGrid view control
Grid view control
 
Chapter12 (1)
Chapter12 (1)Chapter12 (1)
Chapter12 (1)
 
Grid Vew Control VB
Grid Vew Control VBGrid Vew Control VB
Grid Vew Control VB
 
Grid View Control CS
Grid View Control CSGrid View Control CS
Grid View Control CS
 
Ch 7 data binding
Ch 7 data bindingCh 7 data binding
Ch 7 data binding
 
The most basic inline tag
The most basic inline tagThe most basic inline tag
The most basic inline tag
 
Step by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView TutorialsStep by Step Asp.Net GridView Tutorials
Step by Step Asp.Net GridView Tutorials
 
Detail view in distributed technologies
Detail view in distributed technologiesDetail view in distributed technologies
Detail view in distributed technologies
 
AdRotator and AdRepeater Control in Asp.Net for Msc CS
AdRotator and AdRepeater Control in Asp.Net for Msc CSAdRotator and AdRepeater Control in Asp.Net for Msc CS
AdRotator and AdRepeater Control in Asp.Net for Msc CS
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08
 
High performance coding practices code project
High performance coding practices code projectHigh performance coding practices code project
High performance coding practices code project
 
IntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdfIntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdf
 
IntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdfIntoTheNebulaArticle.pdf
IntoTheNebulaArticle.pdf
 
Asp Net Advance Topics
Asp Net Advance TopicsAsp Net Advance Topics
Asp Net Advance Topics
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08
 
Hello Android
Hello AndroidHello Android
Hello Android
 
Ch05 state management
Ch05 state managementCh05 state management
Ch05 state management
 
ASP.Net 3.5 SP1 Dynamic Data
ASP.Net 3.5 SP1 Dynamic DataASP.Net 3.5 SP1 Dynamic Data
ASP.Net 3.5 SP1 Dynamic Data
 
MVVM & Data Binding Library
MVVM & Data Binding Library MVVM & Data Binding Library
MVVM & Data Binding Library
 

Más de application developer (20)

Chapter 26
Chapter 26Chapter 26
Chapter 26
 
Chapter 25
Chapter 25Chapter 25
Chapter 25
 
Chapter 23
Chapter 23Chapter 23
Chapter 23
 
Assignment
AssignmentAssignment
Assignment
 
Next step job board (Assignment)
Next step job board (Assignment)Next step job board (Assignment)
Next step job board (Assignment)
 
Chapter 19
Chapter 19Chapter 19
Chapter 19
 
Chapter 18
Chapter 18Chapter 18
Chapter 18
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
Week 3 assignment
Week 3 assignmentWeek 3 assignment
Week 3 assignment
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Chapter 13
Chapter 13Chapter 13
Chapter 13
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Chapter 11
Chapter 11Chapter 11
Chapter 11
 
Chapter 10
Chapter 10Chapter 10
Chapter 10
 
C # test paper
C # test paperC # test paper
C # test paper
 
Chapter 9
Chapter 9Chapter 9
Chapter 9
 
Chapter 8 part2
Chapter 8   part2Chapter 8   part2
Chapter 8 part2
 
Chapter 8 part1
Chapter 8   part1Chapter 8   part1
Chapter 8 part1
 
Chapter 7
Chapter 7Chapter 7
Chapter 7
 

Último

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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
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
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 

Último (20)

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
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 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
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
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...
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 

Chapter 16

  • 1. In this chapter, you’ll concentrate on three more advanced controls that allow you to bind entire tables of data. • GridView • Details View • FormView
  • 2. GridView: The GridView is an all-purpose grid control for showing large tables of information. it comes equipped with the most ready-made functionality like sorting, paging, selecting, editing and deleting. DetailsView: The DetailsView is ideal for showing a single record at a time. The DetailsView also supports editing. FormView: Like the DetailsView, the FormView shows a single record at a time and supports editing. The difference is that the FormView is based on templates, which allow you to combine fields in a flexible layout that doesn’t need to be table-based. •
  • 3. protected void Page_Load(object sender, EventArgs e) { string connectionString = WebConfigurationManager.ConnectionStrings["Northwind"].Connectio nString; string selectSQL = "SELECT ProductID, ProductName, UnitPrice FROM Products"; SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(selectSQL, con); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds, "Products"); GridView1.DataSource = ds; GridView1.DataBind(); }
  • 4. Of course, you don’t need to write this data access code by hand. Define a SqlDataSource to perform the query shown in the previous example: <asp:SqlDataSource ID="sourceProducts" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind %>" SelectCommand="SELECT ProductID, ProductName, UnitPrice FROM Products" /> Next, set the GridView.DataSourceID property to link the data source to your grid: <asp:GridView ID="GridView1" runat="server" DataSourceID="sourceProducts" />
  • 5. By default, the GridView.AutoGenerateColumns property is true, and the GridView creates a column for each field in the bound DataTable. This automatic column generation is good for creating quick test pages, but it doesn’t give you the flexibility you’ll usually want. For example, what if you want to hide columns, change their order, or configure some aspect of their display, such as the formatting or heading text? In all these cases, you need to set AutoGenerateColumns to false and define the columns in the <Columns> section of the GridView control tag.
  • 6.
  • 7. Now that you understand the underpinnings of the GridView, you’ve still only started to explore its higher-level features. Formatting: How to format rows and data values Selecting: How to let users select a row in the GridView and respond accordingly Editing: How to let users commit record updates, inserts, and deletes Sorting: How to dynamically reorder the GridView in response to clicks on a column header Paging: How to divide a large result set into multiple pages of data Templates: How to take complete control of designing, formatting, and editing by defining templates
  • 8. You handle this job with the DataFormatString property. Each BoundField column provides a DataFormatString property you can use to configure the appearance of numbers and dates using a format string. <asp:BoundField DataField="UnitPrice" HeaderText="Price" DataFormatString="{0:C}" /> <asp:BoundField DataField="BirthDate" HeaderText="Birth Date" DataFormatString="{0:MM/dd/yy}" />
  • 9.
  • 10. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { // Get the price for this row. decimal price = (decimal)DataBinder.Eval(e.Row.DataItem, "UnitPrice"); if (price > 50) { e.Row.BackColor = System.Drawing.Color.Maroon; e.Row.ForeColor = System.Drawing.Color.White; e.Row.Font.Bold = true; } } }
  • 11. The GridView provides built-in support for selection. You simply need to add a CommandField column with the ShowSelectButton property set to true. ASP.NET can render the CommandField as a hyperlink, a button, or a fixed image. <asp:CommandField ShowSelectButton="True" ButtonType="Button“ SelectText="Select" /> <asp:CommandField ShowSelectButton="True" ButtonType="Image“ SelectImageUrl="select.gif" />
  • 12. You don’t need to create a new column to support row selection. Instead, you can turn an existing column into a link. <asp:ButtonField CommandName="Select" ButtonType="Button" DataTextField="ProductID" />
  • 13. A typical master-details page has two GridView controls. The first GridView shows the master (or parent) table. When a user selects an item in the first GridView, the second GridView is filled with related records from the details (or parent) table.
  • 14. Use the CommandField column, set ShowEditButton to true. To enable sorting, you must set the GridView.AllowSorting property to true. Next, you need to define a SortExpression for each column that can be sorted. To use automatic paging, you need to set AllowPaging to true (which shows the page controls), and you need to set PageSize to determine how many rows are allowed on each page.
  • 15. You can use each SqlDataSource control you create, to retrieve a single query. The SqlDataSource command logic is supplied through four properties—SelectCommand, InsertCommand, UpdateCommand, and DeleteCommand—each of which takes a string. <asp:SqlDataSource ID="sourceProducts" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind %>" SelectCommand="SELECT ProductName, ProductID FROM Products“ />
  • 16. <asp:SqlDataSource ID="sourceProductDetails" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:Northwind %>“ SelectCommand="SELECT * FROM Products WHERE ProductID=@ProductID"/> <asp:SqlDataSource ID="sourceProductDetails" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:Northwind %>" SelectCommand="SELECT * FROM Products WHERE ProductID=@ProductID"> <SelectParameters> <asp:ControlParameter ControlID="lstProduct" Name="ProductID" PropertyName="SelectedValue" /> </SelectParameters> </asp:SqlDataSource>
  • 17. If bound to Rich Controls SqlDataSource can display many fields at a time as compared to List Controls which display only one field at a time. Example: Gridview DetailsView
  • 18.
  • 19. For example, you could split the earlier example into two pages. In the first page, define a list control that shows all the available products: <asp:SqlDataSource ID="sourceProducts" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:Northwind %>" SelectCommand="SELECT ProductName, ProductID FROM Products"/> <asp:DropDownList ID="lstProduct" runat="server" AutoPostBack="True" DataSourceID="sourceProducts" DataTextField="ProductName" DataValueField="ProductID" />
  • 20. protected void cmdGo_Click(object sender, EventArgs e) { if (lstProduct.SelectedIndex != -1) { Response.Redirect( "QueryParameter2.aspx?prodID=" + lstProduct.SelectedValue); } } Finally, the second page can bind the DetailsView according to the ProductID value that’s supplied in the query string: <asp:SqlDataSource ID="sourceProductDetails" runat="server" ProviderName="System.Data.SqlClient" ConnectionString="<%$ ConnectionStrings:Northwind %>" SelectCommand="SELECT * FROM Products WHERE ProductID=@ProductID"> <SelectParameters> <asp:QueryStringParameter Name="ProductID" QueryStringField="prodID" /> </SelectParameters> </asp:SqlDataSource> <asp:DetailsView ID="detailsProduct" runat="server" DataSourceID="sourceProductDetails" />