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

The Freeze Pane DataGrid

Rate me:
Please Sign up or sign in to vote.
4.68/5 (63 votes)
14 Nov 2004CPOL3 min read 860.7K   11.9K   189  
Creating a DataGrid with the ability to lock the header row and first column(s).
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ServerAndClientSide.aspx.vb" Inherits="Testing.ServerAndClientSide"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>Client Side Column Locking</title>
		<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
		<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
		<meta content="JavaScript" name="vs_defaultClientScript">
		<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
		<LINK href="Styles.css" type="text/css" rel="stylesheet">
		<script type="text/javascript">
function lockCol(tblID) {

	var table = document.getElementById(tblID);
	var button = document.getElementById('toggle');
	var cTR = table.getElementsByTagName('tr');  //collection of rows

	if (table.rows[0].cells[0].className == '') {
		for (i = 0; i < cTR.length; i++)
			{
			var tr = cTR.item(i);
			tr.cells[0].className = 'locked'
			}
		button.innerText = "Unlock First Column";
		}
		else {
		for (i = 0; i < cTR.length; i++)
			{
			var tr = cTR.item(i);
			tr.cells[0].className = ''
			}
		button.innerText = "Lock First Column";
		}
}
		</script>
	</HEAD>
	<body MS_POSITIONING="FlowLayout">
		<form id="Form1" method="post" runat="server">
			<P>&nbsp;</P>
			<button id="toggle" onclick="lockCol('DataGrid1')" type="button">Unlock First Column</button>
			<div id="div-datagrid">
			<asp:datagrid id="DataGrid1" runat="server" CssClass="Grid" UseAccessibleHeader="True" AutoGenerateColumns=False>
					<AlternatingItemStyle CssClass="GridAltRow"></AlternatingItemStyle>
					<ItemStyle CssClass="GridRow"></ItemStyle>
					<Columns>
						<asp:BoundColumn DataField="Name" HeaderText="Name">
							<ItemStyle Wrap="False"></ItemStyle>
						</asp:BoundColumn>
						<asp:BoundColumn DataField="Address" HeaderText="Address">
							<ItemStyle Wrap="False"></ItemStyle>
						</asp:BoundColumn>
						<asp:BoundColumn DataField="City" HeaderText="City">
							<ItemStyle Wrap="False"></ItemStyle>
						</asp:BoundColumn>
						<asp:BoundColumn DataField="State" HeaderText="State">
							<ItemStyle Wrap="False"></ItemStyle>
						</asp:BoundColumn>
						<asp:BoundColumn DataField="Zip" HeaderText="Zip">
							<ItemStyle Wrap="False"></ItemStyle>
						</asp:BoundColumn>
						<asp:BoundColumn DataField="Random Babble" HeaderText="Random Babble">
						<ItemStyle Wrap="False"></ItemStyle>
						</asp:BoundColumn>
					</Columns>
				</asp:datagrid></div>
			<p></p>
			<p></p>
		</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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
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