Need to get a DataGrid up on the fly? Here's some code from Justin Cook's Behold the Power of the DataGrid!. Here you'll find the code on making a grid, how controlling the way it looks, and a sorting technique. Getting a DataGrid up and running:
<script language="VB" runat="server"> '=== omitted code use to fill a dataset called dsInventory '=== databind to DataGrid called dgSocks dgSocks.DataSource = dsInventory.Tables("socks") dgSocks.DataBind() </script> <asp:DataGrid id="dgSocks" runat="server" />
DataGrid Looks
<script language="VB" runat="server"> '=== omitted code use to fill a dataset called dsInventory '=== databind to DataGrid called dgSocks dgSocks.DataSource = dsInventory.Tables("socks") dgSocks.DataBind() </script> <asp:DataGrid id="dgSocks" runat="server" AutoGenerateColumns="false" width="400" cellPadding="2" Font-Size="10px"> <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>
If you want to get really fancy, you could even add DataFormatString="{0:c}" to the price to print it out in currency format! If you want more information on that, Microsoft has all the answers. But enough with styling, you can play around with that, let's move on to bigger and better things, namely sorting. Sorting
<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( s as Object, e As 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. | 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. |
More ASP.NET Code Articles More By Justin Cook developerWorks - FREE Tools! | You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
| | | | Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
| | | | Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement. FREE! Go There Now!
| | | | Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. FREE! Go There Now!
| | | | XML has become a common way of storing business data as flat files and many data server vendors including IBM have provided ways to store this data within relational database systems. Increasingly collections of XML files are accessed like databases using an xQuery and other XML standard mechanisms. Businesses find the need to combine the traditional tabular structured data with XML formatted data. In this webcast, you’ll learn about IBM’s WebSphere Federation Server technology, which provides users with the ability to integrate these two data formats. FREE! Go There Now!
| | | | Rational Build Forge Express Edition is an automation framework that packages the latest enterprise-grade technologies into a reliable, flexible and robust configuration designed and priced specifically for small to midsize businesses. The new Rational Build Forge Express eKit provides you with valuable resources – including a case study, podcast, demo, and articles – to help you increase staff productivity, compress development cycles and deliver better software, fast. FREE! Go There Now!
| | | | The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size. FREE! Go There Now!
| | | | User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges. FREE! Go There Now!
| | | | Join this webcast to learn how IBM Rational's Functional Testing solution enables you to implement automation your way, at your pace, with your existing staff. In this webcast, you’ll learn how you can eliminate redundancy of manual test scripts, reduce errors, and increase test coverage through test automation. After this presentation you will understand how IBM Rational Functional Testing solution can streamline your manual testing and make test automation easily attainable. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |