DataGrid Code

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.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 289
May 04, 2004
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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
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.

blog comments powered by Disqus
ASP.NET CODE ARTICLES

- How to Use the ListBox Control in ASP.NET 2.0
- How to Load XML Documents in ASP.NET 2.0
- DataGrid Code
- ASP.NET Guestbook
- User Controls and Client Side Scripting
- ASP.NET Programming with Microsoft's AS...
- ASP.NET Basics (part 3): Hard Choices
- ASP.NET Basics (part 2): Not My Type
- ASP.NET Basics (part 1): Nothing But .Net
- Directory Tree Browser
- How to get the confirmation of Yes/No from a...
- Complete example using custom errors and wri...
- Paging Certain # records per page .NET style
- General Methods of formatting and Subtractin...
- .NET LinkButton web control

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials