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! |
<a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. 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!
|
|
|
|
WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities. FREE! Go There Now!
|
|
|
|
Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |