Working with Data Grids IN ASP.NET

The DataGrid control is a bound data control that displays items from the selected data source in a grid or spreadsheet-like fashion. This was possible before .NET, but it required a fair amount of code if you wanted to implement more than a read-only grid.

This new server control, along with the Repeater and DataList controls, makes it a snap to wire up to a data source and display columnar data using a minimal amount of coding.

New to the DataGrid control is paging. You no longer have to be concerned with writing code to handle the paging of data in the grid. For anyone who has written this type of code in the past, this is a welcome relief.

since the DataSource property of the DataGrid control expects the source of data to be derived from the System.Collections.IEnumerable class, you don't have to bind to just SQL or ODBC data. You can also use arrays and collections as the data source for the DataGrid control.

Additional Capabilities when Designing ASPX Pages

Now that you have seen a simple example of using the DataGrid control, you are ready to learn about some of the other capabilities that are available to you when designing ASPX pages:
  1. Controlling the header and footer
  2. Determining the "look and feel" of the grid
  3. Controlling the columns in the grid and specifying what type of column you want to use
  4. Paging
  5. Sorting
As you can see, you have quite a bit of flexibility with the DataGrid control — not only where you get the data, but also how you display that data within your Web pages. Each of these points will be discussed in the sections that follow, with examples of how they work.

Using the Columns Property

The discussion up until this point has addressed how to control the appearance of the grid by setting the various style properties. Also, the examples thus far have let the DataGrid control determine how the columns are generated and in what order. This section looks at how to control what columns are displayed and the type of columns displayed.

When the AutoGenerate property is set to True, the DataGrid control automatically reads the fields from the data source and generates a BoundColumn type for each field. This is useful if you only want to have a quick display of the data; however, to gain control over what fields are displayed and what type of column to display, you need to set the AutoGenerate property to False.

This property is contained within the elements. The following are the column classes that can be defined in the Columns property:

BoundColumn: Displays a column bound to a field in a data source; each item from the data source is displayed in the grid as text. The BoundColumn is the default column type for the DataGrid control.

ButtonColumn: Displays a command button for each item in the column; this control will let you create a column of custom button controls, such as the OK or Cancel button.

EditColumn: For each item in the grid, displays a column that contains editing commands.

HyperLinkColumn: Each item in the column is displayed as a hyperlink; the column contents can be bound to a field in a data source or static text.

TemplateColumn: Each item in the column can be displayed with a specified template; this will allow you to provide custom controls in the column.

Paging Grid Data

Paging came along with the introduction of the DataGrid control. This simple event actually involves quite a bit of work behind the scenes. When the user requests the next page of data, the browser makes a round trip to the server to determine the next page of data, and then renders the next page of data on the client. With .NET, this is now a trivial task, requiring only that you set a few properties and wire up one event. If you want to handle your own paging, you can do that by setting the CustomPaging property.


RELATED POST

CREATING ASSEMBLIES WITH ASP.NET

BUILDING HANDLERS IN ASP.NET

INTERFACES AND CLASSES CREATION IN ASP.NET

CACHING IN ASP.NET

CACHING IN ASP.NET PART TWO

WIRE LESS APPLICATION WITH ASP.NET

SECURITY IN ASP.NET PART ONE

SECURITY IN ASP.NET PART TWO

LOCALIZING ASP.NET APPLICATIONS

DEPLOYING ASP.NET APPLICATIONS

ASP.NET CONFIGURATION SYSTEM

WEB SERVICES IN ASP.NET

WEB SERVICES PART TWO

WEB SERVICE INFRASTRUCTURE

WEB SERVICE INFRASTRUCTURE PART TWO

EXCHANGING MESSAGES IN ASP.NET

MICROSOFT HAIL STORM

SOAP AND DOT NET PART ONE

SOAP AND DOT NET PART TWO


No comments:

Post a Comment