3
In all my grid s I have to define many properties, I wonder if there is any way to define the properties only at one time in a single place.
Grid example:
3
In all my grid s I have to define many properties, I wonder if there is any way to define the properties only at one time in a single place.
Grid example:
0
1 - Creating a class library
to inherit from the class GridView
and put your settings by default
Create a class library project and place 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 through the menu Add Reference
Indicating your class library
. Now configure in your project the directive as follows:
<%@ Register Namespace="Default.WebControl" Assembly="Default.WebControl" TagPrefix="asp" %>
And then you can use the new Gridview created:
Complete 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 various settings and make a call in the Page Load.
2 - Create a class with modifier static
with a method also with modifier static
in that way:
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 example of code only by changing the part of the internal configuration through 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 - A method may also be used static
by reference, thus:
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();
}
}
}
}
Great @Maria! Congratulations!!! I did it through the component...
Browser other questions tagged c# asp.net webforms gridview
You are not signed in. Login or sign up in order to post.
You talk like this, are default properties throughout your grid? if yes put me some example!
– Maria
Hello Maria, sorry if I was unclear... what I meant is that in all the grid s of my project, I define the properties "Gridlines", "Cellpadding", "Pagersettings" and other more. I would like to set this only once for all grids, so when you need to change something I don’t have to in each and change... I was clear?
– Douglas Cristhian
Douglas Cristhian was yes!
– Maria
I made an example @Douglascristhian that you can use, including can have up to two or more extensive methods thus generating more types of Gridview, see if for you this is it.
– Maria
Have you ever studied user Controls? You may have help.
– Marconi