How can I Programmatically insert a Hyperlink into a cell?

5 posts, 0 answers
  1. Robert
    Robert avatar
    9 posts
    Member since:
    Oct 2015

    Posted 13 Oct 2015 Link to this post

    It's a simple one....

     

    How can I Programmatically insert a Hyperlink into a cell?

     

    Thanks.​

  2. Aylin
    Admin
    Aylin avatar
    75 posts

    Posted 14 Oct 2015 Link to this post

    Hello Robert,

    Please check the following code snippet which demonstrates how to insert hyperlinks programmatically: 
    Worksheet worksheet = this.radSpreadsheet.ActiveWorksheet;
    worksheet.Cells[0, 0].SetValueAsText("Telerik");
     
    HyperlinkInfo website = HyperlinkInfo.CreateHyperlink("www.telerik.com", "Telerik");
    CellIndex a1Index = new CellIndex(0, 0);
    worksheet.Hyperlinks.Add(a1Index, website);

    You could find more information on that topic at the Hyperlink article from our online documentation.

    I hope that helps.

    Regards,
    Aylin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Robert
    Robert avatar
    9 posts
    Member since:
    Oct 2015

    Posted 16 Oct 2015 in reply to Aylin Link to this post

    Hi Aylin,

     Thank you for your response. I used searched for 'Hyperlink' on the Telerik Spreadsheet Forum and API Documentation and received zero results. I found essentially the same information you presented me with here by searching Google for 'Excel vba insert hyperlink'.

     What we are actually trying to achieve is to format a cell to look like it contains a hyperlink, but when the user clicks on it, we are going to spawn a new view with additional information. We can achieve all the text formatting we need for this in code behind, but we can't change the cursor to a hand, as is customary for a link. So.. I'm trying to insert a hyperinklink...

    We are wrapping the RadSpreadsheet in a UserControl, but use Prism MVVM and I'm using Prism's InteractionRequest to spawn a new view when a user clicks on a cell with information that can be elaborated on with additional data. In order to get the style I want, I am simply linking to the same cell in the same worksheet that the user clicks on with:

    ...

    for (int i = 0; i < Accounts.Count; i++) {
     
       // for purposes of our example, data containing links will always be in column 3
       // generate a subaddress equivalent to the same cell that contains the hyperlink
       string alphaRow = Convert.ToChar(i + 65).ToString();
       string cell = alphaRow + "3:" + alphaRow + "3";
       HyperlinkInfo link = HyperlinkInfo.CreateInDocumentHyperlink(cell,
         "Click for more about '" + user.Permissions.ToString() + "'");
     
       Debug.WriteLine("Hyperlink inserted at CellIndex: ({0}:3) Place in Doc: {1} link: {2} ",
         i.ToString(),
         cell,
         user.Permissions.ToString());
    }

    Debug shows the links pointing to the right place in the document, but I'm still getting some wierd behavior. After clicking, I'm linked to an unexpected location in the document, or I'm still getting an Invalid Reference error.

    Any suggestions?

  5. Robert
    Robert avatar
    9 posts
    Member since:
    Oct 2015

    Posted 16 Oct 2015 Link to this post

    Ok, I simply added an empty string for the cell reference on the HyperlinkInfo object and it works as expected..

        HyperlinkInfo link = HyperlinkInfo.CreateInDocumentHyperlink(string.Empty, "Click for more about '" +
          user.Permissions.ToString() + "'");
        worksheet.Hyperlinks.Add(new CellIndex(i, 3), link);​

  6. Anna
    Admin
    Anna avatar
    99 posts

    Posted 19 Oct 2015 Link to this post

    Hi,

    Using an empty string will indeed work, because it will create a hyperlink which does nothing when clicked. However, something which is important to note is that this will create an invalid document which you will not be able to export. This, of course, will have to be fixed in the future, but I am concerned that it might interfere with your work at the present moment. I am not sure if your scenario will require export of the document. Please, let me know if you have any thoughts on this.

    Regards,
    Anna
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for WPF is Visual Studio 2017 Ready