Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_professional thread: Extremely Desperate... Datagrid issue.


Message #1 by "Tim Farrell" <timothy.farrell@c...> on Thu, 7 Nov 2002 15:03:14
Hello,

I am attempting to duplicate a technique outlined by Dino Esposito in an 
artcle he wrote some time back about Summary Columns in a datagrid.  I 
have a SP that gathers data from a table and I am attempting to gain 
control of certain columns for formatting.  The article discussed is 
available at:http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dndive/html/data01102002.asp.

I am currently getting an error that reads:
Specified Cast in not valid.

The article was originally written for C# however, Imar Spaanjaars was 
kind enough to translate it to vb for me.

The error in question has something to do with the script dealing with 
ItemCreated.  The article also uses a dataset where as I have implemented 
a stored procedure.

Here is my code:
[code]
<%@Import Namespace="System.Data.SqlClient" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Drawing" %>
<%@Register TagPrefix="pat2002" Tagname="Footer" Src="../../Footer.ascx" %>
<%@Register TagPrefix="pat2002" Tagname="Header" Src="../../Header.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>Line Item - Component - Activity Report</title>
		<script language="JavaScript">
		var keepstatic=1 //specify whether menu should stay static 
0=non static (works only in IE4+)
		var menucolor="#8BAAE1" //specify menu color
		var submenuwidth=100 //specify sub menus' color
		</script>
		<!--#include file="../../Styles.css" -->
		<script language="VB" runat="server">	
 	'****************************************
 	'Function to grab User Identity
 	'****************************************
 	Sub Page_Load
 	If Not IsPostBack Then
 		BindMyDataGrid()
 				
 		lblUID.Text = User.Identity.Name
 	'****************************************
 	'Database Connection String
 	'****************************************
 	Dim strConn as string 
="server=ntphoenix;database=okijuy;uid=sa;pwd=" 
    Dim Conn as new SQLconnection(strConn)
    '*****************************************
    'SQl Script to generate First/Last name for form
    '*****************************************
    Dim strSQL = "SELECT FirstName, LastName FROM Employee WHERE UID = '" 
& lblUID.Text & "'"
    Dim cmd1 as New SQLCommand(strSQL,Conn)
    Conn.Open()
    Dim drUser As SqlDataReader
    Dim sFirstName as String
    Dim sLastName as String
    
    drUser = cmd1.ExecuteReader()
    If drUser.Read() Then
         sFirstName = drUser("FirstName").ToString()
         sLastName = drUser("LastName").ToString()
         lblFirstName.Text = sFirstName
         lblLastName.Text = sLastName
    Else
    ' user not found
	End If
	drUser.Close()
	Conn.Close()
End If
End Sub
Sub BindMyDataGrid()
  Dim strConn as string ="server=ntphoenix;database=okhgty;uid=sa;pwd=" 
  Dim Conn as new SQLconnection(strConn)
  Conn.Open()
  Dim AN As SqlCommand = New SqlCommand("FinalGTH", conn)
  AN.CommandType = CommandType.StoredProcedure
  MyDataGrid.DataSource = AN.ExecuteReader()
  MyDataGrid.DataBind()
  conn.close()
End Sub

Public Sub ItemCreated(ByVal sender As System.Object, ByVal e As 
DataGridItemEventArgs)
            'Get the newly created item
            Dim itemType As ListItemType = e.Item.ItemType

            'Item and AlternatingItem
            If (itemType = ListItemType.Item Or itemType = 
ListItemType.AlternatingItem) Then
                Dim drv As DataRowView = CType(e.Item.DataItem, 
DataRowView)
                If Not (drv Is Nothing) Then
                    'app-speicific way to check if current row is summary 
row
                    If CType(drv("Activity"), Integer) = -1 Then
                        'Modify the row layout
                        e.Item.BackColor = Color.White
                        e.Item.Font.Bold = True
                        e.Item.Cells.RemoveAt(1) 'remove the activity cell
                        e.Item.Cells(0).ColumnSpan = 2 'span the component 
cell
                        e.Item.Cells(1).HorizontalAlign = 
HorizontalAlign.Right
                    End If
                End If
            End If
 End Sub

        'Event Handler: PageIndexChanged
        'Public Sub PageIndexChanged(ByVal sender As System.Object, ByVal 
e As DataGridPageChangedEventArgs)
        '    MyDataGrid.CurrentPageIndex = e.NewPageIndex
        '    UpdateDataView()
        'End Sub

        'Event Handler: ItemDataBound
Public Sub ItemDataBound(ByVal sender As System.Object, ByVal e As 
DataGridItemEventArgs)
            'Retrieve the data linked through the relation
            'Given the structure of the data ONLY ONE row is retrieved
            Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
            If (drv Is Nothing) Then
                Return
            End If

            'Check here the app-specific way to detect whether the
            'current row is a summary row
            If CType(drv("Activity"), Integer) = -1 Then
                If (drv("ComponentID").ToString() = "(Total") Then
                    e.Item.BackColor = Color.Yellow
                    e.Item.Cells(0).Text = "Component Total"
                Else
                    e.Item.Cells(0).Text = "Activity Subtotal"
                    e.Item.BackColor = Color.BlanchedAlmond
                End If
            End If
End Sub

		</script>
		<meta content="True" name="vs_showGrid">
		<meta content="Microsoft Visual Studio.NET 7.0" 
name="GENERATOR">
		<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" 
content="http://schemas.microsoft.com/intellisense/ie5">
	</HEAD>
	<body topmargin="0" bottommargin="0" leftmargin="0" 
rightmargin="0">
		<SCRIPT language="JavaScript" src="../../menu_array.js" 
type="text/javascript"></SCRIPT>
		<SCRIPT language="JavaScript" src="../../mmenu.js" 
type="text/javascript"></SCRIPT>
		<pat2002:Header id="PageHeader" runat="server" />
		<form id="tef" runat="server">
			<table width="100%" cellpadding="2" 
cellspacing="0" border="0">
				<tr class="tabletext">
					<td colspan="3"><b>Report 
Definition:</b> Line Item | Component | Activity</td>
				<tr>
				<tr class="tabletext">
					<td style="FONT-WEIGHT: bold; FONT-
SIZE: 9pt; COLOR: black; FONT-FAMILY: 'Microsoft Sans Serif'">Report 
						Requested By:</td>
					<td align="left"><asp:Label 
ID="lblFirstname" Runat="server" />
						<asp:Label 
ID="lblLastname" Runat="server" />
					</td>
					<td style="FONT-WEIGHT: bold; FONT-
SIZE: 9pt; COLOR: black; FONT-FAMILY: 'Microsoft Sans Serif'">Employee 
						Number:</td>
					<td align="left"><asp:Label 
ID="lblUID" Runat="server" /></td>
					<td width="350"><img 
src="images/pixel.gif"></td>
				</tr>
			</table>
			<table width="100%" cellpadding="0" 
cellspacing="0" border="0">
				<tr>
					<td></td>
				</tr>
				<tr>
					<td>
						<asp:DataGrid 
ID="MyDataGrid" Runat="server" 
						HeaderStyle-
BackColor="#6699ff" 
						HeaderStyle-Font-
Name="verdana" 
						HeaderStyle-Font-
Bold="True" 
						HeaderStyle-Font-Size="xx-
small" 
						AlternatingItemStyle-
BackColor="Gainsboro" 
					
	AutoGenerateColumns="False" 
						AllowCustomPaging="False" 
						AllowPaging="False" 
					
	OnItemCreated="ItemCreated" 
					
	OnItemDataBound="ItemDataBound" 
						GridLines="Both" 
						BorderColor="#000000" 
						Width="70%">
							<Columns>
							
	<asp:BoundColumn DataField="ComponentID" Visible="False" />
							
	<asp:BoundColumn ItemStyle-Font-Size="8" DataField="Component" 
HeaderText="Component" />
							
	<asp:BoundColumn ItemStyle-Font-Size="8" DataField="Activity" 
HeaderText="Activity" />
							
	<asp:BoundColumn ItemStyle-Font-Size="8" DataField="Records" 
HeaderText="Records" />
							
	<asp:BoundColumn ItemStyle-Font-Size="8" DataField="Hours" 
HeaderText="Hours" />
							</Columns>
						</asp:DataGrid>
					</td>
				</tr>
			</table>
		</form>
	</body>
</HTML>
[/code]

If you could please help I would be appreciative.

Thank you in advance.

Sincerely,

Tim

  Return to Index