This is a migrated thread and some comments may be shown as answers.

Hide GridDataItem - Visible False

1 Answer 220 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Allan
Top achievements
Rank 2
Allan asked on 20 Oct 2010, 06:52 PM
I have a very simple Grid that includes a hyperlink column using an image as the link:

<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="sds_Credentials" GridLines="None" Skin="WebBlue" AllowSorting="True" Width="800px">
<MasterTableView AutoGenerateColumns="False" DataKeyNames="IDCredentials" DataSourceID="sds_Credentials">
<CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
    <Columns>
        <telerik:GridBoundColumn DataField="CredentialName" HeaderText="Credential" 
            SortExpression="CredentialName" UniqueName="CredentialName">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="UserLogin" HeaderText="Login" 
            SortExpression="UserLogin" UniqueName="UserLogin">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="UserPassword" HeaderText="Password" 
            SortExpression="UserPassword" UniqueName="UserPassword">
        </telerik:GridBoundColumn>
        <telerik:GridHyperLinkColumn DataNavigateUrlFields="Website" HeaderText=" "
            DataNavigateUrlFormatString="{0}"
            UniqueName="Website" AllowFiltering="False" ItemStyle-HorizontalAlign="Center">
        <ItemStyle Width="50px" CssClass="radgrid" />
        </telerik:GridHyperLinkColumn>
        <telerik:GridHyperLinkColumn DataNavigateUrlFields="IDCredentials" HeaderText=" "
            DataNavigateUrlFormatString="credentials_details.aspx?IDCredentials={0}" 
            Text="Details" UniqueName="IDRolodex" AllowFiltering="False" ItemStyle-HorizontalAlign="Center">
        <ItemStyle Width="75px" CssClass="tbltdbutton" />
        </telerik:GridHyperLinkColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

Code behind:

Protected Sub RadGrid1_ItemDataBound1(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim hyplnk As HyperLink = DirectCast(item("Website").Controls(0), HyperLink)
        hyplnk.ImageUrl = "../../../images/iconIE.gif"
        hyplnk.Target = "_blank"
    End If
End Sub

This works great.

However, I would like to hide the image if there is no hyperlink in the data field. Something like:


Protected Sub RadGrid1_ItemDataBound1(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
        Dim value As String = item("Website").Text
        If value < "1" Then
            Dim hLink As HyperLink = DirectCast(item("Website").Controls(0), HyperLink)
            hLink.ImageUrl = "../../../images/iconIE.gif"
            hLink.Target = "_blank"
            hLink.Visible = False
        End If
    End If
  
End Sub

This does not work. Any help would be much appreciated.

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 21 Oct 2010, 07:19 AM
Hello Allen,

You need to add the DataTextField property of GridHyperLinkColumn. Then in ItemDataBound event access the HyperLink text and check whether it is empty or not. Sample code is given below.

ASPX:
<telerik:GridHyperLinkColumn DataNavigateUrlFields="Website" HeaderText=" "
 DataTextField="Website" DataNavigateUrlFormatString="{0}" UniqueName="Website" >
</telerik:GridHyperLinkColumn>

VB.Net:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = DirectCast(e.Item, GridDataItem)
        Dim hyplnk As HyperLink = DirectCast(item("Website").Controls(0), HyperLink)
        Dim value As String = hyplnk.Text
        hyplnk.ImageUrl = "../../../images/iconIE.gif"
        hyplnk.Target = "_blank"
        If value = "" Then
            hyplnk.Visible = False
        End If
    End If
End Sub

Thanks,
Princy.
Tags
Grid
Asked by
Allan
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Share this question
or