Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FindControl not working

Status
Not open for further replies.

Dimitrie1

Programmer
Jan 5, 2007
138
CA
I have a gridview (gridview2) with a template that contains another gridview (gridview3). I am trying to highlight the last row of gridview3. I used findcontrol but believe I am doing something wrong.

Code:
Protected Sub GridView3_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim irow As Integer
        Dim x As GridView
        x = GridView2.FindControl("GridView3")
        irow = x.Rows.Count - 1
        If irow > 1 Then
            x.Rows(irow).BackColor = Drawing.Color.Cornsilk
        End If
    End Sub
 
Why are you in GridView3_Prerender using Gridview2 trying to find gridview 3 again? Seems to me you are going in circles. You already have access to Gridview3, so you don't need to use FindControl().
 
I tried to just reference gridview3 but it told me gridview3 wasn't declared.

what I really want to do is highlight the last data row a different colour from the rest.
 
You need to use the RowDataBound event of Gridview2 to get a reference(using FindControl()) to Gridview3.
In the HTML for GridView3, add:
Code:
<asp:GridView ID="GridView3" runat="server" [b]OnDataBound="YourEvent"[/b]>
 .......
/asp:GridView>
Add this code:
Code:
Protected Sub YourEvent(ByVal sender As Object, ByVal e As System.EventArgs)
 ...
End Sub
The code in YourEvent will run after all the rows are bound in the grid. You can then loop through and find the last one and highlight it.
 
thanks - I was able to get it to work on the gridview3_rowdatabound

Code:
Protected Sub GridView3_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
       
        If DataBinder.Eval(e.Row.DataItem, "Payment_Type") = "Total" Then
            e.Row.BackColor = Drawing.Color.Cornsilk
        End If
    End Sub
[\code]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top