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