473,383 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Problem viewing text of hyperlink column in datagrid

I am trying to retrieve the text within each cell in a datagrid in
order to change the color of each cell, depending on the value within
that cell.

This works fine on the cells that are bound columns, but returns an
empty string for the cells that are hyperlink columns.

Here is the code to retrive the text:

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Dim x As Integer
For x = 0 To e.Item.Cells.Count - 1
Response.Write(e.Item.Cells(x).Text)
Next
End Sub

I have also tried, which gives me the error:

"Specified argument was out of the range of valid values. Parameter
name: index

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified
argument was out of the range of valid values. Parameter name: index

Source Error:
Line 144: Dim H1 As HyperLink
Line 145: For x = 0 To e.Item.Cells.Count - 1
Line 146: H1 = e.Item.Cells(8).Controls(1)
Line 147: Response.Write(H1.Text)
Line 148: Next"

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Dim x As Integer
Dim H1 As HyperLink
For x = 0 To e.Item.Cells.Count - 1
H1 = e.Item.Cells(8).Controls(1)
Response.Write(H1.Text)
Next
End Sub

This is how I am creating the datagrid:

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
Dim strSql As String = "exec dbo.uspSummaryMatrix"
Dim strCnn As String = "data source=xxxx;integrated
security=false;initial catalog=COPS;uid=xxxx;pwd=xxxx"
objCnn = New SqlConnection(strCnn)
objCmd = New SqlCommand
objCmd.CommandText = "dbo.uspSummaryMatrix"
objCmd.CommandType = CommandType.StoredProcedure
objCmd.Connection = objCnn
objCmd.Parameters.Add(New SqlParameter("@ItemID",
System.Data.SqlDbType.VarChar, 255))

objCmd.Parameters("@ItemID").Value = Request.QueryString("ID")

objCnn.Open()

Dim dReader As SqlDataReader
dReader = Nothing
dReader = objCmd.ExecuteReader()

Dim i As Integer
Dim count As Integer
count = 0

DataGrid1.GridLines = GridLines.Both
DataGrid1.AllowSorting = True
DataGrid1.BorderColor = Color.SlateGray
DataGrid1.BorderStyle = BorderStyle.Solid
DataGrid1.ShowHeader = True
DataGrid1.AutoGenerateColumns = False
DataGrid1.BackColor = Color.White
DataGrid1.AlternatingItemStyle.BackColor = Color.LightGray
DataGrid1.ItemStyle.ForeColor = Color.DarkSlateBlue
DataGrid1.ItemStyle.BackColor = Color.White
DataGrid1.HeaderStyle.Font.Bold = True
DataGrid1.HeaderStyle.ForeColor = Color.White
DataGrid1.HeaderStyle.BackColor = Color.DarkSlateBlue
DataGrid1.Width = Unit.Percentage(100)
DataGrid1.CellPadding = 2
Dim datagridcol
Dim strcolor As String

Do While dReader.Read
If count = 0 Then
For i = 0 To dReader.FieldCount - 1
If InStr(dReader.GetName(i), "container") Then
datagridcol = New BoundColumn
datagridcol.headertext =
Replace(dReader.GetName(i), "c", "C")
datagridcol.datafield = dReader.GetName(i)
DataGrid1.Columns.Add(datagridcol)
End If
If InStr(dReader.GetName(i), "Word") Then
datagridcol = New HyperLinkColumn
datagridcol.HeaderText =
Replace(dReader.GetName(i), "Word", "")
datagridcol.DatatextField = dReader.GetName(i)
End If
If InStr(dReader.GetName(i), "Link") Then
datagridcol.datanavigateurlfield =
dReader.GetName(i)
datagridcol.datanavigateurlformatstring =
"UsageDocuments2.aspx?ID={0}"
datagridcol.target = "iframedocument"
DataGrid1.Columns.Add(datagridcol)
End If
Next
End If
count = count + 1
Loop

dReader.Close()

objDataAdapter = New SqlDataAdapter(objCmd)
objDataSet = New DataSet
objDataView = New DataView
objDataAdapter.Fill(objDataSet)
objDataView.Table = objDataSet.Tables(0)

DataGrid1.DataSource = objDataView
DataGrid1.DataBind()

objCnn.Close()
End Sub


I have also considered creating a template column with a hyperlink
inside the template, but I'm not too sure how to do this or access the
text value within the template column.
Any advice or help is greatly appreciated!

Thank you in advance!
Jill

Nov 21 '05 #1
1 2620
I was able to fix my problem by doing the following. I kept my code
for creating the datagrid the same. Since I know the first column is
always a bound column and the rest are always hypelink columns (and I
don't care what is in the bound column), I am disregarding the first
column all together and starting at 1 in my loop instead of 0.

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Dim x As Integer
Dim h1 As HyperLink
For x = 1 To e.Item.Cells.Count - 1
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Select Case CType(e.Item.Cells(x).Controls(0),
HyperLink).Text
Case Is = "Approved"
e.Item.Cells(x).BackColor = Color.Lime
Case Is = "Denied"
e.Item.Cells(x).BackColor = Color.Red
Case Is = "Identified"
e.Item.Cells(x).BackColor = Color.Cyan
Case Is = "In Work"
e.Item.Cells(x).BackColor = Color.Cyan
Case Is = "Restricted"
e.Item.Cells(x).BackColor = Color.Orange
Case Is = "Retired"
e.Item.Cells(x).BackColor = Color.Orange
Case Is = "Revoked"
e.Item.Cells(x).BackColor = Color.Red
Case Is = "Submitted"
e.Item.Cells(x).BackColor = Color.Yellow
End Select
End If
Next
End Sub

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: George Durzi | last post by:
Hey all, I finally found the necessary resources in the Exchange 2003 SDK to "pull" Contacts out of Exchange and display them on a WebForm. I have been trying to do this forever, and couldn't...
1
by: Soul | last post by:
Hi, I have a column in my DataGrid which is a Hyperlink Column. Everything work fine except when I try to get the text of the column using this.DataGrid.SelectedItem.Cells.Text, I got blank...
6
by: GaryB | last post by:
Does anyone have any idea how to get the text out of a hyperlink column in a web datagrid? It's a hyperlink column, not a template column so findcontrol does not work and there is no apparent...
4
by: Tomek R. | last post by:
Hello ! This post does'nt regard column hyperlink. I just have single hyperlink and want to create it's NavigateUrl dynamically. This is my test page <form id="Form1" method="post"...
3
by: Tim::.. | last post by:
I currently have the following datagrid but want to turn the name and email column into a hyperlink in the codebehind! Can someone please tell me how I achieve this! Thanks Private Sub...
2
by: Fabrice | last post by:
Hello, First, thanks to felix for his answer. But :-( , I'm feeling newbie :! I' don't understand all the situation. The trouble : Always in the road whith my Datagrid and my ItemTemplate...
10
by: david | last post by:
Hi, all: I need a help from you about DataGrid control. I created a DataGrid, dg, in design view of .NET visual Stadio and use the builder to add a Hyperlink column to dg. I want to try to assign...
19
by: Joe | last post by:
I have an aspx page (referred to here as page_1) with a datagrid whose first column contains hyperlinks. When a user clicks one of these hyperlinks, he will navigate to another aspx page (referred...
4
by: Bauer | last post by:
I have a datagrid whose rows contain checkbox and hyperlink columns(containing employee_id field retireved from database). Once the user checks the checkboxes i need to get the employee ids whose...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.