Hi, I have a child gridview with a linkbutton where I'm trying to hide the linkbutton if e.Row.Cells(0) is empty. I'm able to find the linkbutton on a standalone gridview, but nested appears to be different. (edit: current error is "lnkbtn object reference not set to an instance of an object). Thanks in advance.
Protected Sub gvDepartment_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvDepartment.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim id As String = gvDepartment.DataKeys(e.Row.RowIndex).Value.ToString()
Dim gvChild As GridView = TryCast(e.Row.FindControl("gvEmployees"), GridView)
gvChild.DataSource = GetData2(String.Format("SELECT * FROM tblFiles WHERE ID='{0}'", id)) gvChild.DataBind() gvChild.Columns(4).ItemStyle.Wrap = False
Dim lbRT As LinkButton = TryCast(Me.Master.FindControl("Maincontent").FindControl("lnkDownload"), LinkButton)
'e.Row.BackColor = Color.Gray
If gvChild IsNot Nothing Then
Dim lnkbtn As LinkButton = TryCast(e.Row.FindControl("lnkDownload"), LinkButton)
For Each row As GridViewRow In gvChild.Rows
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(0).Text.Equals(" ") Then
lnkbtn.Visible = False
End If
End If
Next
End If
End If
End Sub

Amira BedhiafiRank #601 · 4K reputationPosted Jan 15, 2025, 5:16 PM EST
You're trying to find the
LinkButtonwithin the parentGridView(gvDepartment) instead of the childGridView(gvEmployees). Another thing thelnkbtnobject reference error occurs becauselnkbtnis being searched for in the parent row, where it doesn't exist as expected.Protected Sub gvDepartment_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvDepartment.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then ' Get the primary key value for the current row Dim id As String = gvDepartment.DataKeys(e.Row.RowIndex).Value.ToString() ' Find the child GridView (gvEmployees) inside the current row Dim gvChild As GridView = TryCast(e.Row.FindControl("gvEmployees"), GridView) If gvChild IsNot Nothing Then ' Bind the child GridView with the appropriate data gvChild.DataSource = GetData2(String.Format("SELECT * FROM tblFiles WHERE ID='{0}'", id)) gvChild.DataBind() ' Loop through each row in the child GridView For Each childRow As GridViewRow In gvChild.Rows If childRow.RowType = DataControlRowType.DataRow Then ' Find the LinkButton inside the child GridView Dim lnkbtn As LinkButton = TryCast(childRow.FindControl("lnkDownload"), LinkButton) ' Check if the first cell is empty and hide the LinkButton if needed If childRow.Cells(0).Text.Trim() = "" Then If lnkbtn IsNot Nothing Then lnkbtn.Visible = False End If End If End If Next End If End If End Sub