ASP
  Home arrow ASP arrow Page 7 - ADO.NET 101: Data Rendering with a DataGri...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

ADO.NET 101: Data Rendering with a DataGrid Control
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 78
    2005-11-22

    Table of Contents:
  • ADO.NET 101: Data Rendering with a DataGrid Control
  • Displaying Data from DataReader with a DataGrid Control
  • Connecting to a SQL Server database
  • Adding a SQL Connection
  • Adding a SQLCommand
  • Display formatted data using AutoFormat
  • Formatting via Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    ADO.NET 101: Data Rendering with a DataGrid Control - Formatting via Code


    (Page 7 of 7 )

    You may write the necessary code to format the data which becomes effective at run time. The run time formatting overrides whatever configured at design time. It is also easy to write the code for formatting the DataGrid, because you get visible cues as to the properties to be changed as shown here.

    Code modification for formatting

    The following code added to the data retrieval shown earlier will render the DataGrid as shown in the next picture.

    Private Sub Button1_Click(ByVal sender As System.Object, _ &
    ByVal e As System.EventArgs) Handles Button1.Click
    SqlConnection1.Open()
    Dim dr As SqlClient.SqlDataReader
    dr = SqlCommand1.ExecuteReader
    While dr.Read
    'adding formatting and style With DataGrid1
    .BackColor = System.Drawing.Color.BlueViolet
    .HeaderStyle.ForeColor = 
    System.Drawing.Color.Yellow .CellPadding = 2 .CellSpacing = 3 .ItemStyle.ForeColor =
    System.Drawing.Color.NavajoWhite .HeaderStyle.Font.Bold = True .PagerStyle.ForeColor = System.Drawing.Color.Red .AlternatingItemStyle.BackColor =
    System.Drawing.Color.Turquoise .AlternatingItemStyle.ForeColor =
    System.Drawing.Color.Tomato .BorderStyle = BorderStyle.Groove End With
    DataGrid1.DataSource = dr DataGrid1.DataBind() End While dr.Close() SqlConnection1.Close() End Sub

    Code formatted data in display

    Using the property Builder

    You may also use the PropertyBuidler to build the properties of the DataGrid. In order to access the PropertyBuilder, click the link shown in the DataGrid's property window. This brings up the following window where you can configure the DataGrid's properties.

    For demonstration purposes some property changes were made to the DataGrid's properties in the PropertyBuilder's window. The browser display is as shown below for these settings.

    Formatting with Property Builder

    Summary

    In the present tutorial the DataSource used was a DataReader which supports neither sorting nor paging. There are workarounds to add some additional code, or invoke the other interfaces that support paging, such as ArrayList, or DataTable. This will be considered in another tutorial. The DataGrid user interface is very rich and has a number of ways with which you can change the look and feel of the formatted data, all of which have been covered in the tutorial. As mentioned earlier you may mix the different methods to get the effect you want.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · "When the controls are bound to a data source, the three controls Repeater,...
       · I was away on a holiday where I did not have access to a computer(unbelievable, but...
     

    ASP ARTICLES

    - Using MySQL with ASP
    - ADO for the Beginner
    - ADO.NET 101: Data Rendering with a DataGrid ...
    - Introducing SoftArtisans OfficeWriter 3.0 En...
    - Getting Remote Files With ASP
    - The Real Basics of Functions in ASP
    - Enhancing Readability with ASP
    - Mimicking PHP's String Formatting Functions
    - Windows Server Hacks 12, 77, and 98
    - How to Sort a Multi-Dimensional Array
    - Developing an Information Management Tool wi...
    - What are Active Server Pages?
    - Getting Remote Pages with ASP
    - FTP’ing Files with ASP
    - Apply Single-Sign-On to Your Application





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek