Click here to Skip to main content
15,896,207 members
Articles / Web Development / HTML

GridView with Edit Form (Template)

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
12 Feb 2008CPOL1 min read 74.7K   743   28  
Comfort editing of a GridView row.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;

namespace TemplateGridView
{
    public class TemplateGridView : GridView
    {
        #region EditItemTemplate

        private IBindableTemplate _EditItemTemplate;

        private bool _EnableTemplateEditing;

        public bool EnableTemplateEditing
        {
            get { return _EnableTemplateEditing; }
            set { _EnableTemplateEditing = value; }
        }

        [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(GridViewRow), System.ComponentModel.BindingDirection.TwoWay)]
        public IBindableTemplate EditItemTemplate
        {
            set { _EditItemTemplate = value; }
            get { return _EditItemTemplate; }
        }

        protected override void ExtractRowValues(System.Collections.Specialized.IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
        {
            if (_EnableTemplateEditing)
            {
                IOrderedDictionary dict = _EditItemTemplate.ExtractValues(row.Cells[0]);
                foreach (string s in dict.Keys)
                    fieldValues.Add(s, dict[s]);
            }
            else
                base.ExtractRowValues(fieldValues, row, includeReadOnlyFields, includePrimaryKey);
        }

        protected override void InitializeRow(GridViewRow row, DataControlField[] fields)
        {
            DataControlRowState state = row.RowState & DataControlRowState.Edit;
            if (row.RowType == DataControlRowType.DataRow && state != DataControlRowState.Normal && _EnableTemplateEditing == true)
            {
                int ColSpan = fields.Length;
                row.Cells.Clear();

                TableCell cell = new TableCell();
                cell.ColumnSpan = ColSpan;

                _EditItemTemplate.InstantiateIn(cell);
                row.Cells.Add(cell);
            }
            else
                base.InitializeRow(row, fields);
        }

        #endregion

        protected override void OnInit(EventArgs e)
        {
            if (_EnableTemplateEditing && _EditItemTemplate == null)
                throw new Exception("You have to enter EditItemTemplate to use TemplateEditing!");
            base.OnInit(e);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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

Comments and Discussions