Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: Identifying columns in a DataGrid


Message #1 by "Jon Maz" <jonmaz@s...> on Mon, 4 Mar 2002 20:33:57
Hi,



I have been grabbing the contents of a DataGrid cell by giving the index 

of the column, as follows:



hlViewItemText.NavigateURL="ViewItemText.aspx?ItemID=" & e.Item.Cells

(1).Text



However, as I play around with the layout of the Columns in the DataGrid, 

I have to remember to change the number in the code snippet "e.Item.Cells

(1).Text", so that I am still grabbing data out of the correct column.



This is a bit annoying, so I was hoping I could identify the column not by 

an index number, but by the HeaderText of the column, or by the data the 

column is bound to.



I experimented with syntax like e.Item.Cells("ItemID").Text, but got the 

usual error messages!



Is what I'm trying to do possible?



Thanks,



JON







****************************************************************

	THE CODE

****************************************************************



<%@ Page Language="VB" Debug="true" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.Oledb" %>



<script language="VB" runat="server">

	Dim objConn as New OleDbConnection ( 

ConfigurationSettings.AppSettings("ConnectionString"))

	Dim ds as Dataset = New DataSet()

	Dim objAdapter as New OleDbDataAdapter ( "SELECT * from tblItems", 

objConn )

	

	Sub Page_Load(sender as object, e as eventargs)

		objConn.Open()

		objAdapter.Fill(ds, "tblItems")

		dg.DataSource = ds

		dg.DataMember = "tblItems"

		

		If Not Page.IsPostBack Then

			dg.Databind()

		End If

	End Sub

	

	

	Sub myDataGrid_ItemDataBound(sender As Object, e As 

DataGridItemEventArgs)

	'To add a dynamic hyperlink to the next page under the ItemText 

TextBox

		If (e.Item.ItemType = ListItemType.Item) or 

(e.Item.ItemType = ListItemType.AlternatingItem)

			dim hlViewItemText as new Hyperlink

			hlViewItemText.NavigateURL="ViewItemText.aspx?

ItemID=" & e.Item.Cells(1).Text

		

	'******************************************************************

*

			'Need to make this dynamic, in case I move the 

ItemID column!!!

		

	'******************************************************************

*

			hlViewItemText.Text="ViewItemText"

			e.Item.FindControl

("HyperLinkPlaceHolder").Controls.Add(hlViewItemText)		

		End if

	End sub

		

		

		

	sub dg_edit(sender as object, e as DataGridCommandEventArgs)

		dg.edititemindex = e.item.itemindex

		dg.databind()

	end sub

	

	sub dg_cancel(sender as object, e as DataGridCommandEventArgs)

		dg.edititemindex = -1

		dg.databind()

	end sub

	

	sub dg_update(sender as object, e as DataGridCommandEventArgs)

		'Put something in here later

	end sub

	

</script> 



<html><body>

	<form runat="server">

		<asp:DataGrid id="dg" runat="server"

			Bordercolor="black" 

			gridlines="vertical"

			font-names="Arial" 	

			font-size="10pt"

			cellpadding="4"

	      	cellspacing="0"

	      	width="100%"

			ShowFooter="True"

			HeaderStyle-BackColor="#cccc99"

	      	FooterStyle-BackColor="#cccc99"

			ItemStyle-BackColor="#ffffff"

			AlternatingItemStyle-Backcolor="#cccccc"

			AutoGenerateColumns="False"

			OnItemDataBound="myDataGrid_ItemDataBound"

			OnEditCommand="dg_edit"

			OnCancelCommand="dg_cancel"

			OnUpdateCommand="dg_update"

			>

			

			

			

			<Columns>

				

				<asp:editcommandcolumn HeaderText="EDIT" 

edittext="Edit" CancelText="Cancel" UpdateText="Save" HeaderText="" />

								

				<asp:boundcolumn readonly="true" 

HeaderText="ItemID" DataField="ItemID" />

				<asp:boundcolumn HeaderText="ItemTypeID" 

DataField="ItemTypeID" />

				<asp:boundcolumn readonly="true" 

HeaderText="ItemType" DataField="ItemType" />

				<asp:boundcolumn readonly="true" 

HeaderText="ItemTitle" DataField="ItemTitle" />

				<asp:boundcolumn readonly="true" 

HeaderText="ItemDescription" DataField="ItemDescription" />

				

				

				<%--

				<asp:TemplateColumn ItemStyle-

VerticalAlign="Top" HeaderText="Various">

					<ItemTemplate>

						<b>ItemType: </b><%# 

DataBinder.Eval(Container.DataItem, "ItemTypeID") %> - <%# DataBinder.Eval

(Container.DataItem, "ItemType") %></b><br>

						<b>ItemTitle: </b><%# 

DataBinder.Eval(Container.DataItem, "ItemTitle" ) %><br>   

						<b>ItemDescription: </b><%

# DataBinder.Eval(Container.DataItem, "ItemDescription" ) %><br>

					</ItemTemplate>

				</asp:TemplateColumn>

				--%>

				

				

				<asp:TemplateColumn HeaderText="ItemText">

					<ItemTemplate>

				        <asp:TextBox runat="server" 

ReadOnly="True" Textmode="MultiLine" Columns="30" Rows="8" Text='<%# 

Container.DataItem("ItemText") %>' />

						<br/>

						<asp:Placeholder 

runat="server" id="HyperLinkPlaceholder" />

					</ItemTemplate>

				 </asp:TemplateColumn>

				 

				<asp:boundcolumn HeaderText="PostedBy" 

DataField="PostedBy" />

				<asp:boundcolumn HeaderText="PostedDate" 

DataField="PostedDate" />

				<asp:boundcolumn HeaderText="ItemParent" 

DataField="ItemParent" />

				<asp:boundcolumn HeaderText="SortOrder" 

DataField="SortOrder" />

				<asp:boundcolumn HeaderText="ItemRating" 

DataField="ItemRating" />

				<asp:boundcolumn HeaderText="ModeratedYN" 

DataField="ModeratedYN" />

				<asp:boundcolumn 

HeaderText="JPStillToReplyYN" DataField="JPStillToReplyYN" />

				<asp:boundcolumn 

HeaderText="ArticleOriginalTitle" DataField="ArticleOriginalTitle" />

				

			</Columns>

			

	

		</asp:dataGrid>

		

	</form> 

</body></html>




  Return to Index