Hello frenz,
I am trying to modify the templateField itemized hyperlink on my GridView.
My problem is ,
I have 3 types of status, 'KIV','Complete'&'Incomplete'.
I must display 'KIV' in Red color font with its coresponding linkA.
As for 'Complete', it has to be in 'Green' and matched to linkB.
the 'Incomplete' status should be shown in Black color font and linked it with linkC.

To do this, i used the following method :

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim hLink As String = DataBinder.Eval(e.Row.DataItem, "STATUS").ToString()
If hLink.Equals("KIV") Then

Dim hLnk As HyperLink = CType(e.Row.FindControl("UW Status"), HyperLink)
hLnk.ForeColor = Drawing.Color.Red
hLnk.NavigateUrl = "linkA.asp?AppId=" + DataBinder.Eval(e.Row.DataItem, "APP_ID").ToString()

ElseIf hLink.Equals("Complete") Then

Dim hLnk As HyperLink = CType(e.Row.FindControl("UW Status"), HyperLink)
hLnk.ForeColor = Drawing.Color.Green
hLnk.NavigateUrl = "linkB.asp?AppId=" + DataBinder.Eval(e.Row.DataItem, "APP_ID").ToString()

Else

Dim hLnk As HyperLink = CType(e.Row.FindControl("UW Status"), HyperLink)
hLnk.ForeColor = Drawing.Color.Black
hLnk.NavigateUrl = "linkC.asp?AppId=" + DataBinder.Eval(e.Row.DataItem, "APP_ID").ToString()


End If
End If
End Sub

Problem is, im getting error when i execute the web site.The error description is 'Object reference not set to an instance of an object.'
Plz someoen help me sort it out what is the error with my coding.Realy appreciated.

thanks & regards dev

Does that say what line of code that's happening on?

I can see that you probably just need to instantiate the HyperLink just once:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
	If e.Row.RowType = DataControlRowType.DataRow Then
		Dim hLink As String = DataBinder.Eval(e.Row.DataItem, "STATUS").ToString()
		Dim hLnk As HyperLink = CType(e.Row.FindControl("UW Status"), HyperLink)
	
		If hLink.Equals("KIV") Then
			hLnk.ForeColor = Drawing.Color.Red
			hLnk.NavigateUrl = "linkA.asp?AppId=" + DataBinder.Eval(e.Row.DataItem, "APP_ID").ToString()

		ElseIf hLink.Equals("Complete") Then
			hLnk.ForeColor = Drawing.Color.Green
			hLnk.NavigateUrl = "linkB.asp?AppId=" + DataBinder.Eval(e.Row.DataItem, "APP_ID").ToString()

		Else
			hLnk.ForeColor = Drawing.Color.Black
			hLnk.NavigateUrl = "linkC.asp?AppId=" + DataBinder.Eval(e.Row.DataItem, "APP_ID").ToString()

		End If
	End If
End Sub

Now, if you have that, then what might be going on is that it's not finding a control in your templateField called "UW Status". Alternatively, you might need to use

e.Row.Cells[x].FindControl("UW Status")

where x is the index of the column that you know your templateField is located.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.