Problem viewing text of hyperlink column in datagrid

J

jeguillo

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
 
J

JGeorge

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top