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

Hyperlink column text

5 Answers 550 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Philippe
Top achievements
Rank 2
Philippe asked on 14 Jan 2015, 09:21 PM
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.

5 Answers, 1 is accepted

Sort by
0
Accepted
Hristo
Telerik team
answered on 19 Jan 2015, 01:06 PM
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.

 
0
Philippe
Top achievements
Rank 2
answered on 19 Jan 2015, 03:33 PM
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();
}
0
Hristo
Telerik team
answered on 21 Jan 2015, 02:17 PM
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.

 
0
Todd
Top achievements
Rank 1
answered on 24 Mar 2015, 07:46 PM
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.
0
Hristo
Telerik team
answered on 27 Mar 2015, 04:40 PM
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.

 
Tags
GridView
Asked by
Philippe
Top achievements
Rank 2
Answers by
Hristo
Telerik team
Philippe
Top achievements
Rank 2
Todd
Top achievements
Rank 1
Share this question
or