0% found this document useful (0 votes)
86 views

The Data Controls The Rich Data Controls Include The Following

The document discusses various ASP.NET data controls for binding and displaying data, including the GridView, DetailsView, FormView, and ListView. It provides details on configuring the GridView to automatically generate columns, define columns, format rows and values, select rows, edit data, sort data, and paginate large result sets across multiple pages.

Uploaded by

Arul Jothi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

The Data Controls The Rich Data Controls Include The Following

The document discusses various ASP.NET data controls for binding and displaying data, including the GridView, DetailsView, FormView, and ListView. It provides details on configuring the GridView to automatically generate columns, define columns, format rows and values, select rows, edit data, sort data, and paginate large result sets across multiple pages.

Uploaded by

Arul Jothi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

The Data Controls

When it comes to data binding, not all ASP.NET controls are created equal.
The rich data controls include the following:
GridView: The GridView is an all-purpose grid control for showing large tables of
information.
DetailsView: The DetailsView is ideal for showing a single record at a time, in a
table that has one row per field.
FormView: Like the DetailsView, the FormView shows a single record at a time
and supports editing.
ListView: The ListView plays the same role as the GridView—it allows you to
show multiple records.
The GridView
The GridView is an extremely flexible grid control that displays a multicolumn
table.
Each record in your data source becomes a separate row in the grid.
The GridView is the most powerful of the three rich data controls.
• This functionality includes features for automatic paging, sorting,
selecting, and editing.
Automatically Generating Columns
• The GridView automatically generates a column for every field, as long as
the AutoGenerateColumns property is true.
All you need to create a basic grid with one column for each field:
• <asp:GridView ID="GridView1" runat="server" />
Defining Columns
By default, the GridView.AutoGenerateColumns property is true, and the
GridView creates a column for each field in the bound DataTable.
The most basic column type is BoundField, which binds to one field in the
data object.
Configuring Columns
When you explicitly declare a bound field, you have the opportunity to set
other properties
Property Description

Identifies the field (by name) that you want to display in this
DataField column
Formats the field
DataFormatString
If true, the DataFormat string is used to format the value even
ApplyFormatInEditMode when
the value appears in a text box in edit mode
Sets the text in the header and footer region
FooterText, HeaderText, and
HeaderImageUrl
If true, it prevents the value for this column from being changed
ReadOnly in
edit mode
If true, it prevents the value for this column from being set in
InsertVisible insert
mode
If false, the column won’t be visible in the page
Visible
SortExpression Sorts your results based on one or more columns

If true (the default), all text will be HTML encoded


HtmlEncode
Displays the text that will be shown for a null value
NullDisplayText
If true, converts all empty strings to null values
Generating Columns with Visual Studio
• To configure these details, you need to set
AutoGenerateColumns to false
and define your columns explicitly .
• 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
Formatting the GridView
Formatting consists of several related tasks.
• The GridView supports these features through styles.
Formatting Fields
• Each BoundField column provides a DataFormatString property you can
use to configure the appearance of numbers and dates using a format
string. A typical format string looks something like this:
• {0:C}
Type Format String
{0:C}
Currency
{0:E}
Scientific (Exponential
0:P}
Percentage
{0:F?}
Fixed Decimal
Using Styles
Styles are not simple single-value properties.
Configuring Styles with Visual Studio
• To set style properties, you can use the Properties window to modify the
style properties.
• You can even set a combination of styles using a preset theme by clicking
the Auto Format link in the GridView smart tag.
Formatting-Specific Values
• The solution is to react to the GridView.RowDataBound event. This event
is raised for each row, just after it’s filled with data.
• First, the code checks whether the item being created is an item or an
alternate item.
• To get a value from the bound data object, you need to cast the data
object to the correct type.
Selecting a GridView Row
• Selecting an item refers to the ability to click a row and have it
change color to indicate that the user is currently working
with this record.
• Before you can use item selection, you must define a different
style for selected items.
• The SelectedRowStyle determines how the selected row or
cell will appear.
• To find out what item is currently selected , you can use the
GridView.SelectedIndex property.
• It will be -1 if no item is currently selected.
Using a Data Field As a Select Button
• To use this technique, remove the CommandField column, and add a
ButtonField column instead.
• Then, set the DataTextField to the name of the field you want to use.
• <asp:ButtonField ButtonType="Button" DataTextField="ProductID" />
Using Selection to Create Master-Details Pages
• This trick is a great way to build master-details pages—pages that let you
navigate relationships in a database.
• A typical master-details page has two GridView controls.
Editing with the GridView
• The GridView provides support for editing that’s almost as convenient as
its support for selection.
• To switch a row into select mode, you simply set the SelectedIndex
property to the corresponding row number.
Sorting and Paging the GridView
• The GridView is a great all-in-one solution for displaying all kinds of data,
but it becomes a little unwieldy as the number of fields and rows in your
data source grows.
• Both sorting and paging can be performed by the database server,
provided you craft the right SQL using the Order By and Where clauses.
Sorting
• The GridView sorting features allow the user to reorder the results in the
GridView by clicking a column header. It’s convenient—and easy to
implement.
• The DataView sits between the ASP.NET web page binding and your
DataTable.
• To enable sorting, you must set the GridView.AllowSorting property to
true.
Sorting and Selecting
• select a row, and then sort the data by any column. You’ll see
that the selection will remain, but it will shift to a new item
that has the same index as the previous item.
• In other words, if you select the second row and perform a
sort, the second row will still be selected in the new page,
even though this isn’t the record you selected.
• protected void GridView1_Sorted(object sender,
GridViewSortEventArgs e)
• {
• // Clear selected index.
• GridView1.SelectedIndex = -1;
• }
• protected void GridView1_SelectedIndexChanged(object sender,
EventArgs e)
• {
• // Save the selected value.
• if (GridView1.SelectedIndex != -1)
• {
• ViewState["SelectedValue"] = GridView1.SelectedValue.ToString();
• }
• }
Paging
• Often, a database search will return too many rows to be realistically
displayed in a single page.
• If the client is using a slow connection, an extremely large GridView can
take a frustrating amount of time to arrive
• The GridView handles this scenario with an
automatic paging feature. When you use automatic
paging, the full results are retrieved from the data
source and placed into a DataSet.
• Once the DataSet is bound to the GridView, however,
the data is subdivided into smaller groupings.
• To allow the user to skip from one page to another,
the GridView displays a group of pager controls at
the bottom of the grid
Property Description

AllowPaging Enables or disables the paging of the bound records

PageSize Gets or sets the number of items

PageIndex Gets or sets the zero-based index of the currently displayed page

Provides a PagerSettings object that wraps a variety of formatting options for the
PagerSettings pager controls

Provides a style object you can use to configure fonts, colors, and
PagerStyle text alignment for the paging controls.

You might also like