Hyperlink column text

6 posts, 1 answers
  1. Philippe
    Philippe avatar
    25 posts
    Member since:
    Apr 2012

    Posted 14 Jan 2015 Link to this post

    Hello

    I would like to know how to change the displayed text of an Hyperlink column in an unbound Gridview in C#.

    I added the hyperlink column like this (replaces an already existing column containing the shortcut):
    GridViewHyperlinkColumn hyperlinkColumn = new GridViewHyperlinkColumn("shortcut");
    hyperlinkColumn.ReadOnly = true;
    hyperlinkColumn.Width = 200;
    radGridDocuments.Columns.Remove("shortcut");
    radGridDocuments.Columns.Add(hyperlinkColumn);

    It displays the link correctly, and I am able to click on it to open documents or websites, but I would like to change the text displayed to a more user-friendly text, like simply the document's name instead of the full "\\sharefolder\somefolder\someusername\somedocument.doc", or the full "http://www.telerik.com/forums/winforms/gridview/etc.html".

    I have the text I want displayed in another column, on the same row.

    Thanks.
  2. Answer
    Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 19 Jan 2015 Link to this post

    Hi Philippe,

    Thank you for writing.

    The easiest way to achieve this is by subscribing to CellFormatting event and in the handler change the Text property of the GridCellElement which you receive as an argument.

    I followed your instructions and prepared a small example in which I read the source of a GridHyperlinkCellElement from an existing cell in grid and then deleted the column which contained the existing cell. By subscribing to CellFormatting event I put more user friendly names of the links in the cells. Please see my code snippet below:
    public partial class Form1 : Form
    {
        string nameLink1;
        string nameLink2;
        string nameLink3;
     
        public Form1()
        {
            InitializeComponent();
     
            var dataTable = new DataTable();
            dataTable.Columns.Add("Id", typeof(int));
            dataTable.Columns.Add("Source", typeof(string));
     
            this.radGridView1.DataSource = dataTable;
     
            GridViewHyperlinkColumn hyperlinkColumn = new GridViewHyperlinkColumn("Shortcut");
            hyperlinkColumn.ReadOnly = true;
            hyperlinkColumn.Width = 200;
            this.radGridView1.Columns.Add(hyperlinkColumn);
     
            this.nameLink1 = this.radGridView1.Rows[0].Cells["Source"].Value.ToString();
            this.nameLink2 = this.radGridView1.Rows[1].Cells["Source"].Value.ToString();
            this.nameLink3 = this.radGridView1.Rows[2].Cells["Source"].Value.ToString();
     
            this.radGridView1.Rows[0].Cells["Shortcut"].Value = this.nameLink1;
            this.radGridView1.Rows[1].Cells["Shortcut"].Value = this.nameLink1;
            this.radGridView1.Rows[2].Cells["Shortcut"].Value = this.nameLink1;
     
            this.radGridView1.Columns.Remove("Source");
            this.radGridView1.BestFitColumns(BestFitColumnMode.AllCells);
        }
     
        private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement is GridHyperlinkCellElement)
            {
                if (e.RowIndex == 0 && e.ColumnIndex == 1)
                {
                    e.CellElement.Text = "Hyperlink Column";
                }
                else if (e.RowIndex == 1 && e.ColumnIndex == 1)
                {
                    e.CellElement.Text = "Image Column";
                }
                else
                {
                    e.CellElement.Text = "Textbox Column";
                }
            }
        }
    }

    Additional information on styling and behavior customization you can find here

    I am also sending you a .gif file showing how the grid looks on my side.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Philippe
    Philippe avatar
    25 posts
    Member since:
    Apr 2012

    Posted 19 Jan 2015 in reply to Hristo Link to this post

    Thanks Hristo.

    I don't like having to add even more overhead to my already-packed _CellFormatting function, especially for something that could have been set once at the column's setup. Still, the solution works. Thanks for your help!

    For reference, here is my final code to make it work:
    private void radGridDocuments_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        if (e.RowIndex > -1)
            if ((string)e.CellElement.ColumnInfo.Name == "shortcut")
                e.CellElement.Text = e.Row.Cells["itemName"].Value.ToString();
    }
  5. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 21 Jan 2015 Link to this post

    Hello Philippe,

    Thank you for writing back.

    I am glad that the provided solution worked for your project.

    Have in mind that because of data virtualization the Formatting events are the best place to set the visual appearance of rows, cells etc. Additional information can be found in the following blog post and in this help topic.

    Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Todd
    Todd avatar
    9 posts
    Member since:
    May 2011

    Posted 24 Mar 2015 Link to this post

    Could you provide a code snippet how to change the visited state of the cell element of the hypelink column to default? The info at http://www.telerik.com/help/winforms/gridview-columns-gridviewhyperlinkcolumn.html isn't very helpful.
  7. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 27 Mar 2015 Link to this post

    Hello Todd,

    Thank you for writing.

    You can set a color to the already visited hyperlink cells by handling the CellFormatting event:
    private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        GridHyperlinkCellElement hyperlinkCell = e.CellElement as GridHyperlinkCellElement;
        if (hyperlinkCell != null)
        {
            GridViewHyperlinkCellInfo hyperlinkCellInfo = hyperlinkCell.RowInfo.Cells[e.ColumnIndex] as GridViewHyperlinkCellInfo;
            if (hyperlinkCellInfo != null)
            {
                if (hyperlinkCellInfo.Visited)
                {
                    hyperlinkCell.ContentElement.ForeColor = Color.FromArgb(21, 66, 139);
     
                }
                else
                {
                    hyperlinkCell.ContentElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
                }
            }
        }
    }

    I hope this information is useful. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
Back to Top
UI for WinForms is Visual Studio 2017 Ready