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

Multi-select ASP.NET datagrid

Rate me:
Please Sign up or sign in to vote.
4.57/5 (81 votes)
7 Mar 20042 min read 679.4K   17.6K   165  
How to extend ASP.NET datagrid for multi-selection of data rows.
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Default.aspx.vb" Inherits="WebApplication1.TestMultiselect"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>TestMultiselect</title>
		<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
		<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
		<STYLE type="text/css">H2 { FONT-FAMILY: Verdana,Arial }
	BODY { FONT-FAMILY: Verdana,Arial }
	HR { COLOR: black; HEIGHT: 2px }
	.StdText { FONT-WEIGHT: bold; FONT-SIZE: 9pt; FONT-FAMILY: verdana }
	.StdTextBox { BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; FONT-SIZE: 9pt; FILTER: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true'); BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid; FONT-FAMILY: verdana }
	.Shadow { FILTER: progid:DXImageTransform.Microsoft.dropshadow(OffX=2, OffY=2, Color='gray', Positive='true') }
		</STYLE>
		<!--
'-------------------------------------------------------------
CLIENT SIDE SCRIPT - START
'-------------------------------------------------------------
AIM: Common utilitity functions for client side actions.
BY: Prashant Nayak
DATE: 08/28/03
```````````````````````````````````````````````````````````````
-->
		<script language="javascript">
	<!--
	//-------------------------------------------------------------
	// Select all the checkboxes (Hotmail style)
	//-------------------------------------------------------------
	function SelectAllCheckboxes(spanChk){
	
	// Added as ASPX uses SPAN for checkbox 
	var oItem = spanChk.children;
	var theBox=oItem.item(0)
	xState=theBox.checked;	

		elm=theBox.form.elements;
		for(i=0;i<elm.length;i++)
		if(elm[i].type=="checkbox" && elm[i].id!=theBox.id)
			{
			//elm[i].click();
			if(elm[i].checked!=xState)
			elm[i].click();
			//elm[i].checked=xState;
			}
	}

	//-------------------------------------------------------------
	//----Select highlish rows when the checkboxes are selected
	//
	// Note: The colors are hardcoded, however you can use 
	//       RegisterClientScript blocks methods to use Grid's
	//       ItemTemplates and SelectTemplates colors.
	//		 for ex: grdEmployees.ItemStyle.BackColor OR
	//				 grdEmployees.SelectedItemStyle.BackColor
	//-------------------------------------------------------------
	function HighlightRow(chkB)	{
	var oItem = chkB.children;
	xState=oItem.item(0).checked;	
	if(xState)
		{chkB.parentElement.parentElement.style.backgroundColor='lightcoral';  // grdEmployees.SelectedItemStyle.BackColor
		 chkB.parentElement.parentElement.style.color='white'; // grdEmployees.SelectedItemStyle.ForeColor
		}else 
		{chkB.parentElement.parentElement.style.backgroundColor='white'; //grdEmployees.ItemStyle.BackColor
		 chkB.parentElement.parentElement.style.color='black'; //grdEmployees.ItemStyle.ForeColor
		}
	}
	// -->
		</script>
		<!--
'-------------------------------------------------------------
CLIENT SIDE SCRIPT - END
'-------------------------------------------------------------
// -->
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 13px; POSITION: absolute; TOP: 57px" cellSpacing="2" cellPadding="2">
				<TR>
					<TD align="right">
						<font size="2"><strong>Connection String: </strong></font>
					</TD>
					<TD>
						<asp:textbox id="txtConn" runat="server" cssclass="StdTextBox" text="DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;" width="471px"> DATABASE=Northwind;SERVER= localhost;UID=sa;PWD=;</asp:textbox></TD>
				</TR>
				<TR>
					<TD align="right"><font size="2"><strong>SQL string: </strong></font>
					</TD>
					<TD>
						<asp:textbox id="txtSQL" runat="server" text="DATABASE=Northwind;SERVER=localhost;UID=sa;PWD=;" cssclass="StdTextBox" width="471px">Select EmployeeId, FirstName, LastName, City from Employees</asp:textbox></TD>
				</TR>
				<TR>
					<TD></TD>
					<TD>
						<asp:button id="btnLoad" runat="server" BackColor="Silver" BorderWidth="2px" BorderStyle="Outset" Text="Load" Width="59px" Font-Size="8pt" Height="22px" Font-Names="Verdana"></asp:button></TD>
				</TR>
				<TR>
					<TD colspan="2"><hr>
					</TD>
				</TR>
				<TR>
					<TD colSpan="2">
						<asp:datagrid id="grdEmployees" runat="server" BorderStyle="None" BorderWidth="1px" BackColor="White" BorderColor="#CCCCCC" CellPadding="2" GridLines="Horizontal" ForeColor="Black" Font-Size="8pt">
							<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="LightCoral"></SelectedItemStyle>
							<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#333333"></HeaderStyle>
							<FooterStyle ForeColor="Black" BackColor="#CCCC99"></FooterStyle>
							<Columns>
								<asp:TemplateColumn>
									<HeaderTemplate>
										<asp:CheckBox id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" AutoPostBack="false" ToolTip="Select/Deselect All"></asp:CheckBox><!-- <INPUT onclick="javascript:SelectAllCheckboxes(this);" type="checkbox"> -->
									</HeaderTemplate>
									<ItemTemplate>
										<asp:CheckBox id="chkSelect" onclick="javascript:HighlightRow(this);" runat="server" OnCheckedChanged="grdEmployees_CheckedChanged" AutoPostBack="false"></asp:CheckBox><!-- OnCheckedChanged="grdEmployees_CheckedChanged" onchange="if(this.parentElement.parentElement.style.backgroundColor!='red')this.parentElement.parentElement.style.backgroundColor='red';else this.parentelement.parentelement.style.backgroundcolor='white';" -->
									</ItemTemplate>
								</asp:TemplateColumn>
							</Columns>
							<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="White"></PagerStyle>
						</asp:datagrid></TD>
					<!-- <asp:BoundColumn DataField="EmployeeID" SortExpression="EmployeeID" HeaderText="EmployeeID"></asp:BoundColumn>
								<asp:BoundColumn DataField="FirstName" SortExpression="FirstName" HeaderText="FirstName"></asp:BoundColumn>
								<asp:BoundColumn DataField="LastName" SortExpression="LastName" HeaderText="LastName"></asp:BoundColumn>
								<asp:BoundColumn DataField="City" SortExpression="Title" HeaderText="City"></asp:BoundColumn> -->
				</TR>
				<TR>
					<TD colSpan="2">
						<asp:button id="btnShow" runat="server" BackColor="Silver" BorderWidth="2px" BorderStyle="Outset" Text="Show me my selection" Font-Size="8pt" Font-Names="Verdana" Visible="False"></asp:button></TD>
				</TR>
				<TR>
					<TD colSpan="2">
						<asp:Label id="lblChosen" runat="server" Font-Size="10pt" ForeColor="Green" Font-Bold="True"></asp:Label></TD>
				</TR>
			</TABLE>
			<asp:label id="Label3" style="Z-INDEX: 104; LEFT: 12px; POSITION: absolute; TOP: 13px" runat="server" BackColor="#FFFFC0" Font-Size="14pt" Font-Bold="True">Select and Highlight multiple rows in ASP.NET Datagrid</asp:label>
		</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
Web Developer
United States United States
Cranking code more than 12 years. Technical/Project lead/MCSD. Offered services to various industuries like S/W, Telecom, Publishing, Insurance etc.

When not on computer, I play/swim/read with my kids and help my better half (of course my lovely wife) to clean house. Solving challenging S/W problems is my passion. Hate non-productive meetings. Do lots of GOOGLE and Eat/Drink/Sleep around MSDN.

Comments and Discussions