Blue Theme Orange Theme Green Theme Red Theme
 
Ads by Lake Quincy Media
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » ASP.NET Controls » DataGrid and Datalist Controls in ASP.NET

DataGrid and Datalist Controls in ASP.NET

In this article I will explain DataGrid and Data list Controls in ASP.NET.

Author Rank:
Total page views :  1646
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor


This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".

DataGrid control is one of the most powerful Web controls. By using just a DataGrid control, you can write full-fledged Web applications for your Web site in this section, you'll learn all about the Data Grid methods and properties. The following examples are heavily based on this control.

You can use a DataGrid control to display tabular data. It also provides the ability to insert, update, sort, and scroll the data. Using and binding a Data Grid is ability to insert, update, sort, and scroll the data. Using and binding a Data Grid is easy with the help of the ADO.NET datasets. This grid is capable of auto generating the columns and rows depending on which data you're connecting.

Another control worth mentioning is the DataList control. A DataList control provides a list view of the data from the data source. These controls work similarly.

DataGrid and DataList controls have similar properties. Table 7-5 describes some of the common DataGrid properties.

Table 7-5. Data Grid Control Properties

PROPERTY

DESCRIPTION

Allowpaging, AllowCustomPaging

Boolean values indicate whether paging or custom paging is allowed in the grid. Syntax: AllowPaging ="true"

AllowSorting

Boolean value indicates whether sorting is allowed in the grid. Syntax: AllowSorting ="true"

AutoGenerateColumns

Gets or Sets a value that indicates whether column will automatically be creates for each bound data field. Syntax: AutoGenerateColumns ="true"

BackColor, ForeColor, Font

Sets the background color, foreground color, and font of the grid controlSyntax: BorderColor ="black"; ForeColor = "green"; Font-Name ="Verdana"; Font size ="10pt"

BackImageUrl

Gets or sets the URL of an Image to display in the background of the DataGrid.

BorderColor, BorderStyle, Borderwidth

Sets the border properties of the control. Syntax:
BorderColor ="black"; BorderWidth ="1"

CellPadding, CellSpacing

Sets the cell spacing and padding. Syntax:
CellPadding = "10"; CellSpacing ="5"

Columns

Gets a collection of column controls in the DataGrid.

CurrentPageIndex

Index of the currently displayed page.

DataKeyField

Primary Key field in the data source.

DataSource

Fills the grid with the data. Syntax:
DataGrid1.DataSource = ds.Tables["Student"].DefaultView;

EditorItemIndex

Index of the item to be edited.

EditItemStyle

Style of the item to be edited.

HeaderStyle, FooterStyle

Header and footer styles. Syntax:
HeaderStyleBackColor ="#00aaaa"; FooterStyleBackColor="#00aaaa"

GridLines

Gets or sets the grid line style. Syntax:
Grid Lines ="vertical"

Height, Width

Width and height of the control.

ID

ID of the control.

Page

Returns the page object that contains the current control. Syntax: Page.DataBind();

PageCount, PageSize

Returns the total number of pages and number of item in a page to be displayed. Syntax:
NumPages = ItemGrid.PageCount;Items=ItemGrid.PageCount

SelectedIndex

Index of the currently selected item.

SelectedItem

Returns the selected item in the DataGrid.

ShowFooter, ShowHeader

Show header and footer. Syntax:
ShowFooter ="true";; ShowHeader="true";

UniqueID

Returns the Unique ID for a control.

VirtualItemCount

Total Number of items on the page When AllowCustomerPaging is true.


Setting DataGrid Control Properties at Design-Time

In VS .NET you can set the DataGrid control's properties at design-time by right-clicking on the control and selecting the properties menu item. This displays the Properties window (see figure 7-23).

Figure-7.23.jpg

Figure 7-23. Data Grid properties

Most of these properties should look familiar to you by now. You can set the DataSource property to a data view, dataset, data view manager, or a data reader. The DataMember property represents a column of a data table if you want to display only one column in the control.

AutoFormat

Besides the general properties of a datagrid, in figure 7-23, you see two links: Auto Format and property Builder. The Auto Format option provides you with predefined formats for rows, columns, headers, footers, and so on (see Figure 7-24).

Figure-7.24.jpg

Figure 7-24. DataGrid's Auto Format dialog box

PropertyBuilder

The property Builder option launches the properties dialog box. The first property page is general, as shown in Figure 7-25. This page lets you set a DataSource name at design-time, For Example if you have a DataView control, you can set it as the DataSource property of a grid.

If you want to add headers and footers to the DataGrid control, you can set them by checking the show Header and show Footer check boxes on this page. The Allow Sorting check box enables sorting in a Data Grid control.

Figure-7.25.jpg

Figure 7-25. Data Grid general properties

Column Property page

By default, DataGrid control adds columns at run-time depending upon the DataSource property of the grid. By using the Column property page, you can add DataGrid columns at design-time by unchecking the create Columns Automatically at Run Time check box (see figure 7-26).

Figure-7.26.jpg

Figure 7-26. Data Grid Columns properties page

As you can see in figure 7-26, you even add Data Grid columns as a button a hyperlink, or a template column.

Paging property Page

The paging property page enables the paging feature in the grid control. In other words, this feature allows you to set the number of records displayed per page in a data grid control. For example, if a data grid control has 50 records and you set page size to 10, the data grid will show all 50 records in five pages. The paging property page also allows you to navigate to the first page, next page, previous page, last page, and even to particular page number by selecting the mode of Navigation for navigating pages. You can either display links to the previous and next pages or display specific page numbers (see figure 7-27).

Figure-7.27.jpg

Figure 7-27. DataGrid paging properties page 8

Format property Page

If you don't want to use the Auto Format option, you can use the Format page. This page enables you to set the color and font of a DataGrid control. In fact, you can set the color and the font of the entire grid, headers, footers, and pages (see Figure 7-28).

Figure-7.28.jpg

Figure 7-28. Data Grid Format properties page

Borders property page

The Borders page enables you to set the border, color, font, cell padding and spacing, and type of lines in the Grid (see figure 7-29).

Figure-7.29.jpg

Figure 7-29. DataGrid Border properties page

You'll see how these properties in the sample applications throughout this article.

Binding Web Forms Controls at Design-Time

In the "Developing your first ADO .NET Application" section, you saw how to bind data to Web Forms controls (ListBox and DataGrid, specifically) at run-time Similar to Windows Forms, Web Forms also support design-time data binding. Design-time data binding helps you develop database applications with writing a few lines of code.

Binding data-bound controls at design-time using data adapters and other ADO.NET components is easy. There are many ways to create data adapters at design-time.

In this sample application, you're going to use the Data Adapter Configuration Wizard to create a data adapter. In this example, like always, I'll use the North wind Access 2000 database and the OleDb data provider.

Conclusion

Hope this article would have helped you in understanding
DataGrid and Data list Controls in ASP.NET. See other articles on the website also for further reference.
adobook.jpg
This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.


Login to add your contents and source code to this article
 About the author
 
Puran Mehra

Working as a Software professional. 

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.