Hide GridDataItem - Visible False

2 posts, 1 answers
  1. Allan
    Allan avatar
    121 posts
    Member since:
    Jun 2008

    Posted 20 Oct 2010 Link to this post

    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.

  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 21 Oct 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top