ASP.NET
  Home arrow ASP.NET arrow Page 4 - Behold the Power of the DataGrid!
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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.NET

Behold the Power of the DataGrid!
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 26
    2004-05-03

    Table of Contents:
  • Behold the Power of the DataGrid!
  • What on Earth is a DataGrid?
  • Data Binding
  • Who Wears Sort Sorts?

  • 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


    Behold the Power of the DataGrid! - Who Wears Sort Sorts?


    (Page 4 of 4 )

    I haven't the slightest fondness of my memories of sorting. Whether we're talking about laundry or data, it was a tedious pain in the butt. True, it's just a matter of requesting the proper 'SORT BY' within our SQL statement, but that also involves a lot of client and server scripting to pick up and remember exactly how the user wanted the data sorted. Well, no more I tell you, no more!

    Just a side point here: we could use a DataView object to sort the data, and then bind the DataGrid to that. However, since we're talking about revamping the whole SQL 'SORT BY' method, that's the approach this tutorial will cover.

    Ok, we need to now establish that in order to sort this grid, we're basically going to re-populate the dataset on each sort request, just in a different order. When the page is loaded the first time, the dataset will be sorted in a default manner, by the sock ID.

    Really, all we need to is set the AllowSorting property of the DataGrid to be true, and tell it what to do when it receives an OnSortCommand. We can basically re-set up the initial data-binding subroutine to now pick up a sort value, passed by whichever column is clicked on to sort by, and re-populate the DataGrid! Here's the code:


    <script language="VB" runat="server">
    Sub doBinding
    optional sortBy As String "id" )
     
    '=== omitted code use to fill a dataset called dsInventory
     
     '
    === databind to DataGrid called dgSocks
     dgSocks
    .DataSource dsInventory.Tables("socks")
     dgSocks
    .DataBind()
    End Sub
    Sub reSort
    as ObjectAs DataGridSortCommandEventArgs )
     doBinding
    e.sortExpression )
    End Sub
    </script>

    <asp:DataGrid id="dgSocks" runat="server" AutoGenerateColumns="false"
     width="400" cellPadding="2" Font-Size="10px" 
     AllowSorting="true" OnSortCommand="reSort">
     <HeaderStyle BackColor="Salmon" Font-Bold="true" />
     <Columns>
      <asp:BoundColumn HeaderText="Sock Color" DataField="color" />
      <asp:BoundColumn HeaderText="Price" DataField="price">
       <ItemStyle HorizontalAlign="right" />
      </asp:BoundColumn>
     <Columns>
     <AlternatingItemStyle BackColor="#CCCCCC" />
    </asp:DataGrid>

    So, there's nothing too cryptic about this. When we click on the column to sort the data, the column name is passed back to the doBinding subroutine via the reSort subroutine. Like I said, the default is the ID of the socks. Within our actual SQL statement, we always include the sortBy variable ("SELECT * FROM socks ORDER BY " & sortBy), and that way we don't have to worry if we haven't specified a sort column, the default "id" will be used.  By the way, the default here is to sort in an ascending fashion.

    Now are you beginning to see the incredible power of the DataGrid? This is only the tip of the proverbial iceberg! Just wait until the next installment; your trepidation to migrate to .NET will be out the window.

    Conclusion

    I have introduced you to my new friend the DataGrid. You've started to learn a few of his qualities, his attributes, but the best is yet to come.  The next time around we're going to find out how a DataGrid provides so graciously for our paging and editing requirements. I recommend that in the meantime you get comfortable binding data to a DataGrid, and spit out lots of tables.

    It has been said that absolute power corrupts absolutely. Well, whether or not you choose to be corrupted by the DataGrid is entirely your prerogative, but I still wholeheartedly support this little powerhouse. May you employ the DataGrid gratuitously throughout your programming life, and may your applications live long and prosper!


    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.

     

    ASP.NET ARTICLES

    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX
    - Building a Simple Storefront with LINQ





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT