GridViewHyperlinkColumn
GridViewHyperlinkColumn allows RadGridView to display, format, edit and open hyperlinks as well as run executable files. The default editor of the column is RadTextBoxEditor.
Here is how to create and populate GridViewHyperlinkColumn:
GridViewHyperlinkColumn column = new GridViewHyperlinkColumn("Hyperlink column");
this.radGridView1.Columns.Add(column);
this.radGridView1.Rows.Add("http://www.telerik.com");
this.radGridView1.Rows.Add("http://www.microsoft.com");
this.radGridView1.Rows.Add("http://www.google.com");
this.radGridView1.Rows.Add("http://www.cnn.com");
this.radGridView1.Rows.Add("http://www.bbc.com");
this.radGridView1.Rows.Add("http://www.telerikwatch.com/");
this.radGridView1.Rows.Add("http://www.wikipedia.com");
Dim column As New GridViewHyperlinkColumn("Hyperlink column")
Me.radGridView1.Columns.Add(column)
Me.radGridView1.Rows.Add("http://www.telerik.com")
Me.radGridView1.Rows.Add("http://www.microsoft.com")
Me.radGridView1.Rows.Add("http://www.google.com")
Me.radGridView1.Rows.Add("http://www.cnn.com")
Me.radGridView1.Rows.Add("http://www.bbc.com")
Me.radGridView1.Rows.Add("http://www.telerikwatch.com/")
Me.radGridView1.Rows.Add("http://www.wikipedia.com")
Behavior customization
You can choose the action to open hyperlink or run executable using the HyperlinkOpenAction property of the column. It is an enumeration with the following members:
SingleClick: opens the hyperlink on single mouse click
DoubleClick: opens the hyperlink on double mouse click
None: the user cannot open the link.
The HyperlinkOpenArea property of the column determines whether to execute the hyperlink upon click on the cell or upon click on the text of the cell.
Appearance
The RadGridView theme could define styles for the following GridViewHyperlinkColumn cells states:
Default (unvisited)
Hovered
Clicked
Visited
The mouse cursor transforms into ‘hand’ when hovering hyperlink from the column.
Cell Customization
The hyperlink cells can be further customized through the CellFormating event of the RadGridView. In the event handler, we can check if the e.CellElement property is a GridHyperlinkCellElement element. If yes, we can modify the look of the cell.
private void RadGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
GridHyperlinkCellElement cell = e.CellElement as GridHyperlinkCellElement;
if (cell != null)
{
cell.ContentElement.ForeColor = Color.Red;
}
}
Private Sub RadGridView1_CellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs)
Dim cell As GridHyperlinkCellElement = TryCast(e.CellElement, GridHyperlinkCellElement)
If cell IsNot Nothing Then
cell.ContentElement.ForeColor = Color.Red
End If
End Sub
Events
Here are the GridViewHyperlinkColumn specific events:
HyperlinkOpening: cancelable event which is raised before opening the hyperlink
HyperlinkOpened: event which is raised after opening the link.
The following example demonstrates how to replace the default GridViewTextBoxColumn with a GridViewHyperlinkColumn which stores emails. When an email hyperlink is clicked, a mail message is opened in the default Mail application:
public void SetupEmailsColumn()
{
DataTable table = new DataTable();
table.Columns.Add("textColumn");
table.Rows.Add("asd@asd.com");
table.Rows.Add("qwe@qwe.com");
this.radGridView1.DataSource = table;
// how let's replace the column with a hyperlink column
this.radGridView1.Columns.RemoveAt(0);
GridViewHyperlinkColumn hyperlinkCol = new GridViewHyperlinkColumn();
hyperlinkCol.FieldName = "textColumn";
this.radGridView1.Columns.Add(hyperlinkCol);
this.radGridView1.Columns[0].Width = 200;
this.radGridView1.HyperlinkOpening += RadGridView1_HyperlinkOpening;
}
private void RadGridView1_HyperlinkOpening(object sender, HyperlinkOpeningEventArgs e)
{
System.Diagnostics.Process.Start("mailto:" + e.Cell.Value);
}
Public Sub SetupEmailsColumn()
Dim table As DataTable = New DataTable()
table.Columns.Add("textColumn")
table.Rows.Add("asd@asd.com")
table.Rows.Add("qwe@qwe.com")
Me.radGridView1.DataSource = table
Me.radGridView1.Columns.RemoveAt(0)
Dim hyperlinkCol As GridViewHyperlinkColumn = New GridViewHyperlinkColumn()
hyperlinkCol.FieldName = "textColumn"
Me.radGridView1.Columns.Add(hyperlinkCol)
Me.radGridView1.Columns(0).Width = 200
AddHandler Me.radGridView1.HyperlinkOpening, AddressOf RadGridView1_HyperlinkOpening
End Sub
Private Sub RadGridView1_HyperlinkOpening(ByVal sender As Object, ByVal e As HyperlinkOpeningEventArgs)
System.Diagnostics.Process.Start("mailto:" & e.Cell.Value)
End Sub