Click here to Skip to main content
11,799,282 members (69,334 online)
Click here to Skip to main content

GridView with Edit Form (Template)

, 12 Feb 2008 CPOL 55.1K 627 28
Rate this:
Please Sign up or sign in to vote.
Comfort editing of a GridView row.


When I got my new job, I found out that developers there were still thinking in .NET 1.1, so they used old methods or directly edited HTML in Render methods. One of the controls made in this terrible way was a GridView that used an edit form instead of a classic edit row, and I wanted to make it the new (and I hope, also the right) way.


Let's start designing:

  1. Get an ITemplate (bindable) of the edit form
  2. Insert the template into the row instead of default cells with inputs
  3. Extract the values on postback and use them to update the data source

Points of interest

The control itself is not complicated, so I will not describe all the code, I'll mention only the interesting parts.

  1. I needed a bindable template that can extract the entered values back, so I used the IBindableTemplate and used it as the property.
  2. [PersistenceMode(PersistenceMode.InnerProperty), 
    public IBindableTemplate EditItemTemplate
         set { _EditItemTemplate = value; }
         get { return _EditItemTemplate; }

    The interesting thing about this snippet is the attribute. Ordinary ITemplates provide only one way, so we can not extract the inserted values back, that's why I used the IBindableTemplate and declared two way binding direction in the attribute.

  3. And, the second thing is extracting the values:
  4. protected override void ExtractRowValues(
        System.Collections.Specialized.IOrderedDictionary fieldValues, 
        GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
          if (_EnableTemplateEditing)
              // Not working
              // fieldValues = _EditItemTemplate.ExtractValues(row.Cells[0]);
              // Working
              IOrderedDictionary dict = _EditItemTemplate.ExtractValues(row.Cells[0]);
              foreach (string s in dict.Keys)
                  fieldValues.Add(s, dict[s]);
               base.ExtractRowValues(fieldValues, row, includeReadOnlyFields, 

    The strange thing is that you can not assign the IOrderedDictionary you get from the template to the fieldValues object even if fieldValues is empty. You have to add the values key by key, as shown.

To enable template editing, don't forget to set the EnableTemplateEditing property to true.


  • 2/9/2008 - First version.
  • 2/12/2008 - Download added.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Lukas Holota
Web Developer Barclays Capital
Czech Republic Czech Republic
- .NET Developer, currently working for Barclays Capital
- Student of Czech Technical University in Prague - Faculty of information technologies

You may also be interested in...

Comments and Discussions

GeneralHow would you populate a dropdown? Pin
sacredspirita16-Jun-11 2:17
membersacredspirita16-Jun-11 2:17 
GeneralDB is missing from the download Pin
sacredspirita16-Jun-11 2:05
membersacredspirita16-Jun-11 2:05 
GeneralRe: DB is missing from the download Pin
sacredspirita16-Jun-11 2:16
membersacredspirita16-Jun-11 2:16 
GeneralUserControl as Edit Form Pin
RaYWoLF6-Apr-09 22:12
memberRaYWoLF6-Apr-09 22:12 
GeneralRe: UserControl as Edit Form Pin
Lukas Holota9-Jun-09 9:25
memberLukas Holota9-Jun-09 9:25 
yes, this is normal, because clicking the edit button causes the postback, only the first run is not postback (the grid is usually in read-mode at that time) if you need some more help, send me piece of code and we'll see how I could help Smile | :)

Have questions? try my e-mail l.holota[.at - sign.]

Generalnot saving changes for me Pin
blestab6-Oct-08 0:05
memberblestab6-Oct-08 0:05 
AnswerRe: not saving changes for me Pin
Lukas Holota6-Oct-08 1:01
memberLukas Holota6-Oct-08 1:01 
GeneralGridView with Edit Form (Template) Pin
Dinesh N Samarathunga29-May-08 17:46
memberDinesh N Samarathunga29-May-08 17:46 
GeneralRe: GridView with Edit Form (Template) Pin
Lukas Holota29-May-08 21:51
memberLukas Holota29-May-08 21:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 12 Feb 2008
Article Copyright 2008 by Lukas Holota
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid