Return of the DataGrid: Paging and Editing Explained - Conclusion
(Page 4 of 4 )
I'm really starting to understand the approach Microsoft is taking with the whole ASP.NET platform. If you sit back (please not so far back that you fall off your chair) and stare at your application like on of those 3D pictures, you'll soon see what takes shape. It resembles a Visual Basic Windows application, does it not?
I mean, we haven't touched a bit of HTML code, which was always necessary in ASP 3.0. We're just treating this like any other application, and letting .Net worry about the end result for us. I'm really beginning to enjoy this rapid development idea, I hope you are too! Oh, and just in case you want it, here's all the code for a fully formatted DataGrid with all the bells and whistles:
<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server"></p>
<p>sub page_load( source as Object, e as EventArgs )
if not isPostBack then
doBinding
end if
end sub
Sub doBinding( optional sortBy As String="id" )
'=== omitted code use to fill a dataset called dsInventory
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4;
Data Source=C:Inetpubnewsitetestauto1.mdb"
Dim dbConnection As OleDbConnection = New OleDbConnection(strConn)
Dim strSQL As String = "SELECT * FROM socks "
strSQL &= " ORDER BY " & sortBy
Dim dataAdapter As IDbDataAdapter = New OleDbDataAdapter( strSQL, strConn)
Dim dataSet As DataSet = New DataSet
dataAdapter.Fill(dataSet)
'=== databind to DataGrid called dgSocks
dgSocks.DataSource = dataSet
dgSocks.DataBind()
End Sub
sub pager(s as Object, e as DataGridPageChangedEventArgs )
dgSocks.CurrentPageIndex = e.NewPageIndex
doBinding
end sub
Sub reSort( s as Object, e As DataGridSortCommandEventArgs )
doBinding( e.sortExpression )
End Sub
sub editMode( s as Object, e As DataGridCommandEventArgs )
dgSocks.EditItemIndex = e.Item.ItemIndex
doBinding
End sub
sub cancelEdit( s as Object, e As DataGridCommandEventArgs )
dgSocks.EditItemIndex = -1
doBinding
End sub
sub doUpdate( s as Object, e As DataGridCommandEventArgs )
Dim strConn, strSQL as String
dim updCMD as OleDbCommand
Dim intID, intQuantity as Integer
Dim dblPrice as Double
intID = dgSocks.DataKeys.Item(e.Item.ItemIndex)
dblPrice = cDbl((CType( e.Item.Cells(2).Controls(0), Textbox)).Text)
intQuantity = cInt((CType( e.Item.Cells(3).Controls(0), Textbox)).Text)
strSQL = "UPDATE socks SET price = " & dblPrice.ToString & _
", quantity = " & intQuantity.ToString & _
" WHERE id = " & intID.ToString
'response.write( strSQL)
'response.end()
strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:Inetpubnewsitetestauto1.mdb"
Dim dbConnection As OleDbConnection = New OleDbConnection(strConn)
dbConnection.Open()
'dbConnection.Execute( strSQL )
updCMD = new OleDbCommand( strSQL, dbConnection )
updCMD.ExecuteNonQuery
dbConnection.Close()
dgSocks.EditItemIndex = -1
doBinding
end sub</p>
<p></script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:DataGrid id="dgSocks" runat="server" AutoGenerateColumns="false"
width="400" cellPadding="2" Font-Size="10px"
AllowSorting="true" OnSortCommand="reSort"
AllowPaging="true" PageSize="3" OnPageIndexChanged="pager"
PagerStyle-HorizontalAlign="center" PagerStyle-Mode="NumericPages"
DataKeyField="id" onEditCommand="editMode" onUpdateCommand="doUpdate"
OnCancelCommand="cancelEdit">
<HeaderStyle backcolor="Salmon" font-bold="true" />
<Columns>
<asp:EditCommandColumn EditText="mod" UpdateText="upd" CancelText="cancel"
/>
<asp:BoundColumn HeaderText="Sock Color" DataField="color" readonly="true"
/>
<asp:BoundColumn HeaderText="Price" DataField="price" DataFormatString="{0:c}">
<ItemStyle horizontalalign="right" />
</asp:BoundColumn>
<asp:BoundColumn HeaderText="Quantity" DataField="quantity" />
</Columns>
<AlternatingItemStyle backcolor="#CCCCCC" />
</asp:DataGrid>
</form>
</body>
</html>
| 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. |