Gridview inheriting properties

In all my grids I have to set many properties, I would like to know if there is any way to set the properties only at once in a single place.

Grid example:

insert the description of the image here

Author: bfavaretto, 2014-08-27

1 answers

1 - Creating a class library to inherit from Class GridView and default its settings

Create a class library project and put this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Default.WebControl
{
    [ControlValuePropertyAttribute("SelectedValue")]
    [ToolboxData("GridViewDefault")]
    public class GridViewDefault : GridView
    {
        public GridViewDefault()
        {
            this.GridLines = GridLines.Both;
            this.CellPadding = 5;
            this.CellSpacing = 6;
            this.Width = new Unit("100%");
        }
    }
}

After this creation of the library class make the reference in your Web project by the menu Add Reference

insert the description of the image here

Indicating ai to its class library. Now configure the directive in your project as follows:

<%@ Register Namespace="Default.WebControl" Assembly="Default.WebControl" TagPrefix="asp" %>

And then you can normally use the new GridView created notice:

insert the description of the image here

Full Code Aspx :

<%@ Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="WebApplicationForms.About" Debug="true" %>
<%@ Register Namespace="Default.WebControl" Assembly="Default.WebControl" TagPrefix="asp" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: Title %>.</h2>
    <h3>Your application description page.</h3>
    <p>Use this area to provide additional information.</p>
    <asp:GridViewDefault ID="GridViewDefault1" runat="server"></asp:GridViewDefault>       
    <asp:Button ID="BtnAtualizar" OnClick="BtnAtualizar_Click" runat="server"  />
</asp:Content>

With extensive method you can even assemble several settings and make a call in the page Load.

2 - Create a class with modifier static with a method also with modifier static like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace System.Web.UI.WebControls
{
    public static class Methods
    {
        public static void RenderConfiguration(this GridView grid)
        {
            grid.GridLines = GridLines.Both;
            grid.CellPadding = 5;
            grid.CellSpacing = 6;
            grid.Width = new Unit("100%");
        }
    }
}

Note: Follow this code example only by changing the part of the internal configuration using your own settings.

No Load:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplicationForms
{
    public partial class About : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.RenderConfiguration(); // chamando configuração padrão!
                GridView1.DataSource = new object[]{
                    new {Id = 1, Nome = "GridView 1"},
                    new {Id = 2, Nome = "GridView 2"},
                    new {Id = 3, Nome = "GridView 3"},
                    new {Id = 4, Nome = "GridView 4"},
                    new {Id = 5, Nome = "GridView 5"}
                };
                GridView1.DataBind();
            }
        }
    }
}

3 - you can also use a method static by reference, like this:

using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace System.Web.UI.WebControls
{
    public static class Methods
    {        
        public static void RenderConfiguration(ref GridView grid)
        {
            grid.GridLines = GridLines.Both;
            grid.CellPadding = 5;
            grid.CellSpacing = 6;
            grid.Width = new Unit("100%");
        }
    }
}

No Load:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplicationForms
{
    public partial class About : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                Methods.RenderConfiguration(ref GridView1); // chamando por referencia
                GridView1.DataSource = new object[]{
                    new {Id = 1, Nome = "GridView 1"},
                    new {Id = 2, Nome = "GridView 2"},
                    new {Id = 3, Nome = "GridView 3"},
                    new {Id = 4, Nome = "GridView 4"},
                    new {Id = 5, Nome = "GridView 5"}
                };
                GridView1.DataBind();
            }
        }
    }
}
 0
Author: Maria, 2014-08-28 14:02:41