.NET
  Home arrow .NET arrow Page 4 - Return of the DataGrid: Paging and Editing...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Return of the DataGrid: Paging and Editing Explained
By: Justin Cook
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 24
    2004-05-05

    Table of Contents:
  • Return of the DataGrid: Paging and Editing Explained
  • Paging
  • Editing
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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_loadsource as Objectas 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
    (as Objectas DataGridPageChangedEventArgs )

    dgSocks
    .CurrentPageIndex e.NewPageIndex

    doBinding

    end sub



    Sub reSort
    as ObjectAs DataGridSortCommandEventArgs )

    doBinding
    e.sortExpression )

    End Sub



    sub editMode
    as ObjectAs DataGridCommandEventArgs )

    dgSocks
    .EditItemIndex e.Item.ItemIndex

    doBinding

    End sub



    sub cancelEdit
    as ObjectAs DataGridCommandEventArgs )

    dgSocks
    .EditItemIndex = -1

    doBinding

    End sub



    sub doUpdate
    as ObjectAs 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((CTypee.Item.Cells(2).Controls(0), Textbox)).Text)

    intQuantity 
    cInt((CTypee.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.

     

    .NET ARTICLES

    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway