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

Display GridHyperlinkColumn as regular text

4 Answers 512 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Karl
Top achievements
Rank 1
Karl asked on 13 Dec 2010, 04:18 PM
Here's one for all you out there... probably quite a simple solution to this but I can't think of it!

Suppose I have a RadGrid and in that grid I want to display my data which has 10 rows. Some of my data (say 5 rows of it) has more details which I want to display in a separate page, so I use a hyperlink column so that when a user clicks the link, theyre taken to the alternate page (the one showing the other informtion for the item).

However, the rest of my data (the other 5 rows) doesn't have any more information associated with it, so the link is useless, but I still want to show the data in the grid for that column (it could be a part number or description). I dont want to disable my hyperlink as that would look odd.

My question is this...

How do I get none linkable row data to show as regular text instead of a hyperlink in a grids hyperlink column?

thanks,
Karl

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 14 Dec 2010, 07:00 AM
Hello Karl,

Give a try with the following approach to achieve this.

ASPX:
<telerik:GridHyperLinkColumn DataTextFormatString="{0}" DataNavigateUrlFields="CompanyName"
  DataNavigateUrlFormatString="/sites/abc/xyz/vhj/Pages/view.aspx?id={0}&ReturnUrl=/Pages
  /Search.aspx" UniqueName="GridHyperLinkColumn" DataTextField="CompanyName" >
</telerik:GridHyperLinkColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           if ()//your condition
           {
               HyperLink link = (HyperLink)item["GridHyperLinkColumn"].Controls[0];
               link.NavigateUrl = "";
           }
       }
   }

Thanks,
Princy.
0
Karl
Top achievements
Rank 1
answered on 14 Dec 2010, 11:28 AM
Thanks for pointing me in the right direction Princy...

I've got a bit of an issue though. As my grid is part of a RadPanelItem template I have to dynamically add the event handler for ItemDataBound and already have some code that works in the event.

After the code that already exists and works in the event handler, I have added the code you recommended but get the following error when trying to cast my dataItem to the class type that the grid gets bound to, so I can check the boolean value which decides if I show the text as a link or not...

#  (TemplateItemLine)item.DataItem Cannot cast '((Telerik.Web.UI.GridItem)(item)).DataItem' (which has an actual type of 'DynamicClass1') to 'ServiceTrack.Net.TemplateItemLine' ServiceTrack.Net.TemplateItemLine

Obviously I have to cast it to my type because at design time my grid has no idea what is being bound to it.
0
Karl
Top achievements
Rank 1
answered on 15 Dec 2010, 12:33 PM
I'm still not able to cast my DataItem to the correct type so I can do my check, but as I've been testing I've removed this check and tried the solution given to me.

I noticed that the link, though the NavigateUrl has been cleared, still appears as a link. I've had a look at the CSS using IE's developer tools and created a "DisabledLink" class in my CSS that looks like the regular grid text, and applied this as the same time as clearing the NavigateUrl field and it looks awesome! Exactly what I was after.

All I need to do now is have the cast work... Does anyone have any ideas?

Karl
0
Karl
Top achievements
Rank 1
answered on 15 Dec 2010, 01:58 PM
Cracked it...

I found a post on an ASP.Net forum very similar where a user wanted to get the value of a dynamicClass property but was unable to cast it to their desired class type.

Using reflection you can cycle through the properties of the DynamicClass and search for the property, then check its value. I've wrapped this up in a method and call that in my check and shown here...

private void GridControl_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
            GridDataItem item = (GridDataItem)e.Item;
        if (FindBooleanPropertyValue(item.DataItem, "MyBooleanProperty") == false)
        {
            HyperLink link = (HyperLink)item["DescriptionColumn"].Controls[0];
            link.NavigateUrl = "";
            link.CssClass = "DisabledLink";
        }
    }
}
  
private bool FindBooleanPropertyValue(object o, string propertyName)
{
    bool returnValue = false;
  
    foreach (PropertyInfo prop in o.GetType().GetProperties())
    {
        if (prop.CanRead && prop.Name == propertyName)
        {
            returnValue = (bool)prop.GetValue(o, null);
            break;
        }
    }
  
    return returnValue;
}
Tags
Grid
Asked by
Karl
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Karl
Top achievements
Rank 1
Share this question
or