Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
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 :
Page Views : 6914
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate 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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.