Add Hyperlink to TableCell

2 posts, 0 answers
  1. Alex
    Alex avatar
    1 posts
    Member since:
    Jun 2013

    Posted 24 Jul 2014 Link to this post

    I'm attempting to add a Hyperlink to a Table and need some guidance on how to implement correctly. Below is the method that I'm using that I modified from another post. When it runs it appears to add the row correctly however all I see are blank lines in the cells that contain the Hyperlinks. 

    01.private TableRow AddNewRow(object[] items)
    02.{
    03.    TableRow newRow = new TableRow();
    04.    for (int i = 0; i < items.Length; i++)
    05.    {
    06.        TableCell cell = new TableCell();
    07.        Paragraph paragraph = new Paragraph();
    08.        Span span = new Span();
    09.         
    10.        if(items[i] is string)
    11.        {
    12.            var newString = items[i] as string;
    13.            if (string.IsNullOrEmpty(newString)) newString = "No available information";
    14.            span.Text = newString;
    15.            paragraph.Inlines.Add(span);
    16.        }
    17.        else if(items[i] is HyperlinkInfo)
    18.        {
    19.            var hyperlinkInfo = items[i] as HyperlinkInfo;
    20.            HyperlinkRangeStart hyperlinkStart = new HyperlinkRangeStart();
    21.            hyperlinkStart.HyperlinkInfo = hyperlinkInfo;
    22.            HyperlinkRangeEnd hyperlinkEnd = new HyperlinkRangeEnd();
    23.            hyperlinkEnd.PairWithStart(hyperlinkStart);
    24.            span.Text = hyperlinkInfo.ToString();
    25.            paragraph.Inlines.Add(hyperlinkStart);
    26.            paragraph.Inlines.Add(hyperlinkEnd);
    27.        }
    28.        cell.Blocks.Add(paragraph);
    29.        newRow.Cells.Add(cell);
    30.    }
    31.    return newRow;
    32.}
  2. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 29 Jul 2014 Link to this post

    Hello Alex,

    Thank you for writing.

    You need to insert some text or image between the HyperlinkRangeStart and the HyperlinkRangeEnd, which will represent the actual item which the user needs to click (pressing the Ctrl key) to open the hyperlink. Here is a sample code snippet:
    public Form1()
    {
        InitializeComponent();
        CreateTable();
    }
     
    private void CreateTable()
    {
        Section section = new Section();
        Table table = new Table();
     
        TableRow row1 = new TableRow();
        TableCell cell1 = new TableCell();
        Paragraph p1 = new Paragraph();
        Span s1 = new Span();
        s1.Text = "Cell 1";
        p1.Inlines.Add(s1);
        cell1.Blocks.Add(p1);
        row1.Cells.Add(cell1);
     
        TableCell cell2 = new TableCell();
        Paragraph p2 = new Paragraph();
        Span s2 = new Span();
        s2.Text = "Cell 2";
        p2.Inlines.Add(s2);
        cell2.Blocks.Add(p2);
        row1.Cells.Add(cell2);
        table.Rows.Add(row1);
     
        TableRow row2 = new TableRow();
        TableCell cell3 = new TableCell();
        cell3.ColumnSpan = 2;
        Paragraph p3 = new Paragraph();
        Span s3 = new Span();
        s3.Text = "Cell 3";
        p3.Inlines.Add(s3);
        cell3.Blocks.Add(p3);
        row2.Cells.Add(cell3);
        table.Rows.Add(row2);
        section.Blocks.Add(table);
        this.radRichTextBox1.Document.Sections.Add(section);
    }
     
    private void radButton1_Click(object sender, EventArgs e)
    {
        Section tableSection = this.radRichTextBox1.Document.Sections.First();
        Table table = tableSection.Blocks.First() as Table;
        if (table != null)
        {
            TableRow newRow = new TableRow();
            TableCell cell = new TableCell();
            cell.ColumnSpan = 2;
            HyperlinkRangeStart hyperlinkStart = new HyperlinkRangeStart();
            HyperlinkRangeEnd hyperlinkEnd = new HyperlinkRangeEnd();
            hyperlinkEnd.PairWithStart(hyperlinkStart);
            HyperlinkInfo hyperlinkInfo = new HyperlinkInfo()
            {
                NavigateUri = "http://www.telerik.com",
                Target = HyperlinkTargets.Blank
            };
            hyperlinkStart.HyperlinkInfo = hyperlinkInfo;
     
            Paragraph paragraph = new Paragraph();
            Span span = new Span("Hyperlink");               
            paragraph.Inlines.Add(hyperlinkStart);
            paragraph.Inlines.Add(span);
            paragraph.Inlines.Add(hyperlinkEnd);
            cell.Blocks.Add(paragraph);
            newRow.Cells.Add(cell);
            table.Rows.Add(newRow);
        }
    }

    Please refer to our Hyperlink help article, which is useful about this topic.

    I hope this information helps. Should you have further questions, I would be glad to help.
     
    Regards,
    Desislava
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top