CreateControlStyle Method

DataGrid.CreateControlStyle Method

Creates new control style.

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

protected override Style CreateControlStyle()

Return Value

Type: System.Web.UI.WebControls.Style
A Style the represents the new style.

The following code example demonstrates how to override the CreateControlStyle method in a custom server control so that it always displays the GridLines with no CellSpacing in the DataGrid.

<%@ Page language="c#" %>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
    <title>Custom DataGrid - CreateControlStyle - C# Example</title>
    <form id="Form1" method="post" runat="server">
      <h3>Custom DataGrid - CreateControlStyle - C# Example</h3>
      <aspSample:CustomDataGridCreateControlStyle id="Datagrid1" runat="server" AutoGenerateColumns="False">
          <asp:HyperLinkColumn Text="" Target="_blank" HeaderText="HyperLinks" NavigateUrl="" />


using System.Web;
using System.Security.Permissions;
namespace Samples.AspNet.CS.Controls
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class CustomDataGridCreateControlStyle : System.Web.UI.WebControls.DataGrid
        protected override System.Web.UI.WebControls.Style CreateControlStyle()
            // Create a new TableStyle instance based on ViewState values.
            System.Web.UI.WebControls.TableStyle style = new System.Web.UI.WebControls.TableStyle(ViewState);

            // Show the GridLines with no CellSpacing.
            style.GridLines = System.Web.UI.WebControls.GridLines.Both;
            style.CellSpacing = 0;

            // Return the Style 
            return style;

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1
© 2015 Microsoft