This documentation is archived and is not being maintained.

GridViewDesigner Class

Provides design-time support in a visual designer for the GridView control.

Namespace:  System.Web.UI.Design.WebControls
Assembly:  System.Design (in System.Design.dll)

public class GridViewDesigner : DataBoundControlDesigner

In a visual designer, when you switch from Source to Design view, the markup source code that describes the GridView control is parsed and a design-time version of the control is created on the design surface. When you switch back to Source view, the design-time control is persisted to the markup source code and edited into the markup for the Web page.

The properties of the GridViewDesigner class provide the following functionality:

  • The ActionLists property returns a DesignerActionListCollection object, which typically contains an object that is derived from the DesignerActionList class for each level in the inheritance tree of the designer.

  • The AutoFormats property returns a collection of formatting schemes for display in the Auto Format dialog box.

  • The TemplateGroups property returns a collection of template groups for the fields of the associated GridView control and the top-level GridView templates.

  • The UsePreviewControl property always returns true, indicating that the designer creates a temporary copy of the associated GridView to generate the design-time markup.

The methods of the GridViewDesigner class provide the following functionality:

Design-time editable regions are not supported in the GridView control, so the GetEditableDesignerRegionContent and SetEditableDesignerRegionContent methods have no functionality.

The following code example shows how to extend the GridViewDesigner class to change the appearance of controls that are derived from the GridView control at design time.

The example derives the MyGridView control from the GridView. The MyGridView is simply a copy of the GridView. The example also derives the MyGridViewDesigner class from the GridViewDesigner class and places a DesignerAttribute object for the MyGridViewDesigner on the MyGridView control.

The MyGridViewDesigner overrides the PreFilterProperties method to make the Page property visible in the Properties grid at design time. It overrides the GetDesignTimeHtml method to include the Caption property, if it is specified, as a new first row in the MyGridView control at design time. If the BorderStyle property of the MyGridView control has the NotSet or None value, the GetDesignTimeHtml draws a blue dashed border around the control to make its extent more visible.

using System;
using System.Web;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Web.UI.Design.WebControls;
using System.Collections;
using System.ComponentModel;
using System.Security.Permissions;

namespace Examples.CS.WebControls.Design
{
    // The MyGridView is a copy of the GridView.
    [AspNetHostingPermission(SecurityAction.Demand, 
        Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, 
        Level = AspNetHostingPermissionLevel.Minimal)]
    [Designer(typeof(Examples.CS.WebControls.Design.MyGridViewDesigner))]
    public class MyGridView : GridView
    {
    } // MyGridView 

    // Override members of the GridViewDesigner.
    [ReflectionPermission(SecurityAction.Demand, Flags=ReflectionPermissionFlag.MemberAccess)]
    public class MyGridViewDesigner : GridViewDesigner
    {
        // Shadow the control properties with design-time properties. 
        protected override void PreFilterProperties(IDictionary properties)
        {
            // Call the base method first. 
            base.PreFilterProperties(properties);

            // Make the Page visible in the Properties grid.
            PropertyDescriptor selectProp = 
                (PropertyDescriptor)properties["Page"];
            properties["Page"] =
                TypeDescriptor.CreateProperty(selectProp.ComponentType, 
                    selectProp, BrowsableAttribute.Yes);
        } // PreFilterProperties 

        // Generate the design-time markup. 
        const string capTag = "caption";
        const string trOpen = "tr><td colspan=9 align=center";
        const string trClose = "td></tr";

        public override string GetDesignTimeHtml()
        {
            // Make the full extent of the control more visible in the designer. 
            // If the border style is None or NotSet, change the border to 
            // a wide, blue, dashed line. Include the caption within the border.
            MyGridView myGV = (MyGridView)Component;
            string markup = null;
            int charX;

            // Check if the border style should be changed. 
            if (myGV.BorderStyle == BorderStyle.NotSet ||
                myGV.BorderStyle == BorderStyle.None)
            {
                BorderStyle oldBorderStyle = myGV.BorderStyle;
                Unit oldBorderWidth = myGV.BorderWidth;
                Color oldBorderColor = myGV.BorderColor;

                // Set the design-time properties and catch any exceptions. 
                try
                {
                    myGV.BorderStyle = BorderStyle.Dashed;
                    myGV.BorderWidth = Unit.Pixel(3);
                    myGV.BorderColor = Color.Blue;

                    // Call the base method to generate the markup.
                    markup = base.GetDesignTimeHtml();
                }
                catch (Exception ex)
                {
                    markup = GetErrorDesignTimeHtml(ex);
                }
                finally
                {
                    // Restore the properties to their original settings.
                    myGV.BorderStyle = oldBorderStyle;
                    myGV.BorderWidth = oldBorderWidth;
                    myGV.BorderColor = oldBorderColor;
                }
            }
            else 
                // Call the base method to generate the markup.
                markup = base.GetDesignTimeHtml();

            // Look for a <caption> tag. 
            if ((charX = markup.IndexOf(capTag)) > 0)
            {
                // Replace the first caption with  
                // "tr><td colspan=9 align=center".
                // It is okay if the colspan exceeds the  
                // number of columns in the table.
                markup = markup.Remove(charX,
                    capTag.Length).Insert(charX, trOpen);

                // Replace the second caption with "td></tr".
                if ((charX = markup.IndexOf(capTag, charX)) > 0)
                    markup = markup.Remove(charX,
                        capTag.Length).Insert(charX, trClose);
            }
            return markup;

        } // GetDesignTimeHtml
    } // MyGridViewDesigner
} // Examples.CS.WebControls.Design

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: