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! |
This demonstration gives you an overview of IBM® Rational® Build Forge Express Edition, a global offering that provides a framework to automate and execute software processes. Rational Build Forge provides a software assembly line that can support all of your tools, technologies, and platforms so you can achieve a repeatable, reliable, and traceable build and release process. FREE! Go There Now!
|
|
|
|
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!
|
|
|
|
Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base. FREE! Go There Now!
|
|
|
|
As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> FREE! Go There Now!
|
|
|
|
IBM DB2 9.5 provides new options for tighter security, and allows for more granularity and flexibility in administration of the database. This tutorial is the first of two tutorials that cover roles and trusted contexts. Follow the exercises in this tutorial, and learn how to take advantage of the new DB2 feature roles in combination with other essential e-business technologies such as Web services, Web application server, and DB2 database server. FREE! Go There Now!
|
|
|
|
Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered! FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered! FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
|
|
|
|
Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |