Click here to Skip to main content
15,892,674 members
Articles / Web Development / ASP.NET

Hide DataGrid Columns via HeaderText

Rate me:
Please Sign up or sign in to vote.
4.50/5 (19 votes)
20 Jul 20052 min read 212.6K   1.4K   63  
Shows a developer how to dynamically hide or show DataGrid columns.
<%@ Page language="c#" Codebehind="DgColVisibility.aspx.cs" AutoEventWireup="false" Inherits="DgColWebApp.DgColVisibility" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>DgColVisibility</title>
		<meta name="vs_showGrid" content="False">
		<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
	<body>
		<form id="frmDgColVisibility" method="post" runat="server">
			<table>
				<tr>
					<td></td>
				</tr>
				<tr>
					<td>
						<asp:Button id="btnUser" runat="server" Text="User View"></asp:Button>&nbsp;
						<asp:Button id="btnAdminView" runat="server" Text="Admin View"></asp:Button>&nbsp;Current 
						View =
						<asp:Label id="lblUiView" runat="server"></asp:Label></td>
				</tr>
				<tr>
					<td>
						<asp:DataGrid id="dgExample" runat="server" AutoGenerateColumns="False">
							<AlternatingItemStyle BackColor="LightSteelBlue"></AlternatingItemStyle>
							<HeaderStyle HorizontalAlign="Center" BackColor="RoyalBlue"></HeaderStyle>
							<Columns>
								<asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn>
								<asp:BoundColumn DataField="Title" HeaderText="Title"></asp:BoundColumn>
								<asp:BoundColumn DataField="Phone" HeaderText="Phone"></asp:BoundColumn>
								<asp:BoundColumn DataField="Email" HeaderText="Email"></asp:BoundColumn>
								<asp:BoundColumn DataField="Salary" HeaderText="Salary"></asp:BoundColumn>
								<asp:BoundColumn DataField="SSNum" HeaderText="SS#"></asp:BoundColumn>
							</Columns>
						</asp:DataGrid>
					</td>
				</tr>
			</table>
		</form>
	</body>
</HTML>

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions