.NET
  Home arrow .NET arrow Page 3 - 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 
Mobile Linux 
App Generation ROI 
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 - Editing


    (Page 3 of 4 )

    I honestly can't tell you the number of times I've had to do supply editing capabilities within a table of data. This involves having hidden fields to track the row (item) you wish to edit, the actual text fields to hold the values, and the server-side intelligence to pick up the modification and commit it to the database. Not the most difficult thing in the world to do, but tedious nonetheless.

    Well, wouldn't you know it, Microsoft has been so gracious as to make our lives easier in this arena as well! Unfortunately it's not as simple as typing AllowEdit=”true”. There is slightly more required than just one or two lines, but it's still pretty sweet. Basically all we need to work with is three properties: OnEditCommand, OnCancelCommand, and OnUpdateCommand. There is also an OnDeleteCommand if you need it. And to make it all work, we just need to tell the DataGrid which field is the key for each row, which we do by use of the DataKeyField property.

    So, as we add these to our DataGrid, we see it look something like this:



    <asp:DataGrid id="dgSocks" runat="server" AutoGenerateColumns="true" 

    DataKeyField
    ="id" onEditCommand="editMode" onUpdateCommand="doUpdate" 

    OnCancelCommand
    ="cancelEdit">


    Now, we see a bunch of subroutines being referenced here: editMode(), doUpdate(), and cancelEdit(). Let's first worry about editMode() and cancelEdit().



    sub editModeas ObjectAs DataGridCommandEventArgs )

    dgSocks
    .EditItemIndex e.Item.ItemIndex

    doBinding

    End sub
    </p>
    <
    p> </p>
    <
    p>sub cancelEditas ObjectAs DataGridCommandEventArgs )

    dgSocks
    .EditItemIndex = -1

    doBinding

    End sub


    Let me explain what's going on here. As soon as you turn on editing as we have, you get an extra column in your table. In each row is an edit 'button'. When we click edit for a given row, the 'item index' is passed to the editMode() subroutine, which in turn understands and signals that you want to edit that specific row, so put it in edit mode! What this means is that all modifiable fields are replaced by text boxes containing the modifiable values. And the edit 'button' is replaced by Cancel and Update.

    So go ahead and try this. Make sure you have created an empty doUpdate() subroutine to circumvent any compile-time errors.

    What happens? Well, hopefully exactly what I described! Now if you have paging still turned on, try paging while in edit mode. You'll see that the edit mode stays on, and now the same row on the next page is being edited. This is because it's the same index, make sense?

    So how do we get out of edit mode? Try clicking update. Does that do it? It shouldn't, because you've got nothing within the doUpdate() subroutine to tell it to. But if you click cancel, we can see the Edit Item Index is changed to -1, thus exiting edit mode.

    Alright, now that we have that under our belt, we just need to create the logic behind the actual updating of the item. Here's what doUpdate should look like:



    sub doUpdateas ObjectAs DataGridCommandEventArgs )

    Dim strConn
    strSQL as String

    dim updCMD 
    as OleDbCommand

    Dim intID
    intQuantity as Integer

    Dim dblPrice 
    as Double</p>
    <
    p> </p>
    <
    p>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)</p>
    <
    p> </p>
    <
    p>strSQL "UPDATE socks SET price = " dblPrice.ToString _

    ", quantity = " intQuantity.ToString _

    " WHERE id = " intID.ToString

    strConn 
    "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data" 
    "Source=C:Inetpubdbinventory.mdb"

    Dim dbConnection 
    As OleDbConnection = New OleDbConnection(strConn)

    dbConnection
    .Open()

    'dbConnection.Execute( strSQL )

    updCMD = new OleDbCommand( strSQL, dbConnection )

    updCMD.ExecuteNonQuery

    dbConnection.Close()</p>
    <p> </p>
    <p>dgSocks.EditItemIndex = -1

    doBinding

    end sub


    So by now, you should be able to read through that like the morning paper. Essentially we're just picking up the modified values, throwing them into a SQL UPDATE statement, and executing it. Then we set the edit item index back to -1 (exit edit mode), and re-bind the data to pick up all the fresh values.

    What may have stumped you (though it may not have) is the part where we get the textbox values. Really, all I did there was cast the Control(0) of the cell to a textbox. This is just a quick way of obtaining the text value, which could then be cast to whatever data type necessary.

    And that's all it takes! Are you sold yet?

    More .NET Articles
    More By Justin Cook


     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - 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





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT