GridView with Insert Section - My Step-by-Step Notes (Almost a Tutorial)

Rate It (2)

Last post 07-12-2009 4:03 PM by johnly. 4 replies.

Sort Posts:

  • GridView with Insert Section - My Step-by-Step Notes (Almost a Tutorial)

    01-24-2006, 6:12 PM
    • Member
      100 point Member
    • JLibertor
    • Member since 11-11-2005, 6:30 PM
    • Posts 22

    Though I have been programming for over 10 years, I am a complete .NET noob.  I have been working with GridViews for several weeks and after much blood and tears(literally) I have finally figured out how to use GridViews with ObjectData Sources. 

    Over the past three weeks several specific problems have cost me 3-4 days to figure out.  To organize myself, I typed up a step-by-step instruction manual for creating a GridView page (with an Insert section) using my company's provided ObjectData Sources.

    While the following may not be a step-by-step instruciton applicable to your specific situation, there are many little gems that may save you fellow .Net noobs a few days of frustration!

    If you disagree with me on how to do something then .. well you are probablty right and I would appreciate you to set me straight!

    So here it is in all of it's rawness.


     

     Note on Refreshing Class Libraries

    If you have your objects squirreled away in a class library, then it may be hard to get yoru changes to show up in the designer. 

    Every time a change is made in a class library the changes will not show up in the design pager until you:

    1. Open the class library and press CTRL-ALT-B (to rebuild)
    2. Open the DateSource's Smart Tag on the Design View and choose Refresh Schema
    3. Some popups will jump out - DO NOT SAY YES TO EITHER QUESTION ABOUT RELOAING, unless you want to erase all work so far.

     Note: If nothing happens when you press Refresh Schema, then it didn’t work.  This is not uncommon.  Recompile the whole project, or delete the files in the Bin folder. Try again.

     

    Background on the DATA Objects

    The GridView accepts objects as rows and will use these objects to display the editable grid. However, (to my great surprise) when you edit the row and hit “Save” the GridView DOES NOT pass back the actual object. Instead it constructs a new object from scratch and fills in only the columns explicitly displayed/handled in the GridView.

     

    Therefore a Business-layer object need to receive the infant object/row and beef it up.

     

    Example:

    You have a collection of records. When instantiated they are quite full with all sorts of meta-data like – date of last change, created-by user. Some of these fields are to be calculated, and some of these fields are to be sent on a round-trip.  Create BIZ methods objects that will take in the infantile object, handle all the columns explicitly. (you can also use the DataKeyNames  property - explained later)

     

    1. Create the BIZ Insert methods in the BIZ object
      1. Will handle all fields that need to be explicitly calculated or handled.
    2. Create the BIZ Update methods in the BIZ object
      1. Will handle all fields that need to be explicitly calculated or handled.
    3. Drag a new ObjectDataSource to the page and configure it
      1. Match up the Select, Insert_BIZ, and Update_BIZ methods to their respective events.

    UPDATE Section

    1. Create a GridView
      1. Link to the ObjectDataSource created above
    2. Using the Property bar: Add all round-trip hidden fields to DataKeyNames Property
      1. For example Record ID's
      2. (If the ID's are read-only then... well... your screwed...get creative.)
    3. Click GridViewTasks -> Edit Columns…
      1. Delete Cols not to be displayed (Biz method will handle them)
      2. Reorder the cols (for aesthetics)
      3. Rename the headers (for aesthetics)
      4. Make the non-editable fields ReadOnly
    4. Make the Public Resolve Fields for the ID’s (i.e. you want to display UserName “Joe” instead of UserID “23343”) 

    I found it necessary to add actual new properties to the object for columns that require differetn data. It seems that the GridView wants 100% of the data reconcilled before it is handed over. I named them stuff like BIZ_UserName, and BIZ_WarehouseName

     

      1. Add new properties to the SELECT class

                                                                   i.      Create a Get method

    1.      return string based on private data

    a.       (don’t forget to check for errors and scrub data)

                                                                 ii.      Create an empty Set statement (GridView crashes otherwise)

      1. For each column that needs a resolved version

                                                                   i.      Turn the column into a template, and close

                                                                 ii.      Edit Binding for the Item Template

                                                                iii.      Change the bound field

      1. If the column is not handled by the Biz Update Method, add it to the DataKeyNames Property (else it will not round-trip)
    1. Determine the fields that need a Dropdown for the editing portion
      1. For each

                                                                   i.      Make the field into a Template and close – probably already a template from last step

                                                                 ii.      Open the templates and Delete the input field in the EditItemTemplate

                                                                iii.      Create a SQL data object (or whatever you want) - and configure it to pull up the appropriate data for the dropdown

    1.      Note: Don’t add the DataObject into the actual template field, add it on the main page so that it can be reused by other dropdowns if needed.

    2.      Rename the data object to something meaningful

                                                               iv.      Drag a Dropdown to the EditItemTemplate

                                                                 v.      Choose data source for the object

    1.      Choose the Value and Display fields

                                                               vi.      Set the SelectedID

    1.      Choose Data Bindings for the DropDown

    2.      Set the SelectedValue property to the ID of the previously deleted field

    INSERT Section

    1. Drag and Drop a Detail View
    2. Choose Data Source - >  ObjectDateSource for the GridView (The same as above)
    3. Edit Fields
      1. Rearrange (for asthectics)
      2. Rename headers (for asthectics)
    4. For each field that needs a dropdown
      1. Edit Field -> Change to Template
      2. Open the INSERT template
      3. Delete the text box
      4. DnD a Dropdown
      5. Change DataSource to the ALREADY existing data source used for dropdowns in the GridView above
      6. Important –

                                                                   i.      Chose Edit Bindings

                                                                 ii.      Bind the Dropdown to the appropriate field

    ( This one really took me a while to figure out. You’d think that since we are only doing Inserts here, we don’t need to bind to a SelectedID. If you do not bind to the correct field, then your dropdown will always return 0! )

     

    1. On the GridView Enable Inserts (using the smart tag checkbox)
    2. Switch to Source Mode and edit the DetailView to start off in Insert Mode
      1. <asp:DetailsView ... DefaultMode="Insert">

     


    Miscellaneous

    • To have a dropdown allow blank selections you have to UNION in a blank entry, else the bind crashes. For example: 
      • SELECT [ProductID], [ProductName] FROM [CRM_Products]

    UNION ALL

    SELECT '', ''

    ORDER BY ProductID

  • Re: GridView with Insert Section - My Step-by-Step Notes (Almost a Tutorial)

    01-03-2009, 7:33 AM
    • Contributor
      3,472 point Contributor
    • rami_nassar
    • Member since 10-07-2008, 7:01 AM
    • U.A.E.
    • Posts 788

    hope if you can put this taturial in articles websites like http://www.codeproject.com and put it with detailed code & images so it will be more readble and useful.. & just here provide us with a link to it....

    nice & helpful taturial, thanks

    Regards...
    Nassar, Rami (MCP, MCTS)
    My Blog || E-Mail

    Don't forget to click "Mark as Answer" on the post that helped you.

    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: GridView with Insert Section - My Step-by-Step Notes (Almost a Tutorial)

    06-17-2009, 7:23 AM
    • Participant
      1,091 point Participant
    • brijmohans10
    • Member since 06-17-2009, 6:30 AM
    • India
    • Posts 228

     is it 3-tier arch.???  b'cause object datasource is used in 3-tier arch.

    Thanks
    brij
    Remember to click “Mark as Answer” on the post, if it helps you.
  • Re: hiii

    06-17-2009, 7:27 AM
    • Participant
      1,091 point Participant
    • brijmohans10
    • Member since 06-17-2009, 6:30 AM
    • India
    • Posts 228

     i think you should go to www.asp.net/learn/dataaccess.

    Thanks
    brij
    Remember to click “Mark as Answer” on the post, if it helps you.
  • Re: GridView with Insert Section - My Step-by-Step Notes (Almost a Tutorial)

    07-12-2009, 4:03 PM
    • Member
      292 point Member
    • johnly
    • Member since 05-31-2008, 8:23 AM
    • Bangalore, India
    • Posts 56

    Hi JLibertor,

    Thanks for this useful article. I was searching for this for a long time. it really helped.

    Your article and the below article helped me.

    http://forums.asp.net/p/889109/939477.aspx

     

     

     

    Johnlee Sam
    Zebralive | Send BulkSMS
Page 1 of 1 (5 items)