Back to Devexpress

GridView.HyperlinkClick Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-grid-dot-gridview-3b3d0aa4.md

latest8.5 KB
Original Source

GridView.HyperlinkClick Event

Fires when a hyperlink in a column header is activated.

Namespace : DevExpress.XtraGrid.Views.Grid

Assembly : DevExpress.XtraGrid.v25.2.dll

NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Action")]
public event EventHandler<GridHyperlinkClickEventArgs> HyperlinkClick
vb
<DXCategory("Action")>
Public Event HyperlinkClick As EventHandler(Of GridHyperlinkClickEventArgs)

Event Data

The HyperlinkClick event's data class is DevExpress.XtraGrid.Views.Grid.GridHyperlinkClickEventArgs.

Remarks

The OptionsView.AllowHtmlDrawHeaders property enables the use of HTML tags to format text in the following elements:

You can use HTML tags to insert hyperlinks. The default behavior is to display hyperlinks, but restrict their activation on mouse events.

Set the OptionsBehavior.HyperlinkClickMode property to Click to allow users to activate hyperlinks on a mouse click. Set this property to CtrlClick to activate hyperlinks on a mouse click combined with the CTRL key.

Handle the HyperlinkClick event (BandedGridView.HyperlinkClick for Banded Grid Views) to perform actions when a hyperlink is activated.

Example

The following example displays a hyperlink in the header of the ‘Change’ grid column. The GridView.HyperlinkClick event is handled to respond to a link click — which toggles the display mode of this column’s values.

csharp
private void Form1_Load(object sender, EventArgs e) {
    BindingList<Ticker> list = new BindingList<Ticker>();
    list.Add(new Ticker() { Symbol = "AVR", Price = 101.4m, AbsoluteChange = 2.3m });
    list.Add(new Ticker() { Symbol = "RVA", Price = 414.1m, AbsoluteChange = 3.2m });
    list.Add(new Ticker() { Symbol = "ARBV", Price = 532.7m, AbsoluteChange = -18.0m });

    gridControl1.DataSource = list;
    gridView1.OptionsView.AllowHtmlDrawHeaders = true;
    gridView1.OptionsBehavior.HyperlinkClickMode = DevExpress.Utils.Drawing.HyperlinkClickMode.CtrlClick;
    gridView1.HyperlinkClick += GridView1_HyperlinkClick;

    gridView1.Columns["AbsoluteChange"].Visible = false;
    gridView1.Columns["PercentageChange"].Visible = false;

    GridColumn changeColumn = gridView1.Columns.AddUnbound("Change", typeof(decimal));
    changeColumn.Visible = true;
    changeColumn.OptionsColumn.AllowEdit = false;
    updateChangeColumnDisplayMode();

    gridView1.Columns["Price"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
    gridView1.Columns["Price"].DisplayFormat.FormatString = "c2";
}

bool usePercentageFormat = false;

private void GridView1_HyperlinkClick(object sender, DevExpress.XtraGrid.Views.Grid.GridHyperlinkClickEventArgs e) {
    if (e.Link == "toggle-display-mode") {
        usePercentageFormat = !usePercentageFormat;
        updateChangeColumnDisplayMode();
    }
}

void updateChangeColumnDisplayMode() {
    GridColumn column = gridView1.Columns["Change"];
    if (column == null) return;
    column.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
    if (usePercentageFormat) {
        column.Caption = "Change <a href=toggle-display-mode>(%)</a>";
        column.DisplayFormat.FormatString = "p2";
        // Display values from the PercentageChange field.
        column.UnboundExpression = "PercentageChange";
    }
    else {
        column.Caption = "Change <a href=toggle-display-mode>($)</a>";
        column.DisplayFormat.FormatString = "c2";
        // Display values from the AbsoluteChange field.
        column.UnboundExpression = "AbsoluteChange";
    }
}

public class Ticker {
    public string Symbol { get; set; }
    public decimal Price { get; set; }
    public decimal AbsoluteChange { get; set; }
    public decimal PercentageChange {
        get {
            if (Price > 0) return AbsoluteChange / Price;
            else return 0;
        }
    }
}
vb
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim list As New BindingList(Of Ticker)()
    list.Add(New Ticker() With {
            .Symbol = "AVR",
            .Price = 101.4D,
            .AbsoluteChange = 2.3D
        })
    list.Add(New Ticker() With {
            .Symbol = "RVA",
            .Price = 414.1D,
            .AbsoluteChange = 3.2D
        })
    list.Add(New Ticker() With {
            .Symbol = "ARBV",
            .Price = 532.7D,
            .AbsoluteChange = -18D
        })

    GridControl1.DataSource = list
    GridView1.OptionsView.AllowHtmlDrawHeaders = True
    GridView1.OptionsBehavior.HyperlinkClickMode = DevExpress.Utils.Drawing.HyperlinkClickMode.CtrlClick
    AddHandler GridView1.HyperlinkClick, AddressOf GridView1_HyperlinkClick

    GridView1.Columns("AbsoluteChange").Visible = False
    GridView1.Columns("PercentageChange").Visible = False

    Dim changeColumn As GridColumn = GridView1.Columns.AddUnbound("Change", GetType(Decimal))
    changeColumn.Visible = True
    changeColumn.OptionsColumn.AllowEdit = False
    updateChangeColumnDisplayMode()

    GridView1.Columns("Price").DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
    GridView1.Columns("Price").DisplayFormat.FormatString = "c2"
End Sub

Private usePercentageFormat As Boolean = False

Private Sub GridView1_HyperlinkClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.GridHyperlinkClickEventArgs)
    If e.Link = "toggle-display-mode" Then
        usePercentageFormat = Not usePercentageFormat
        updateChangeColumnDisplayMode()
    End If
End Sub

Private Sub updateChangeColumnDisplayMode()
    Dim column As GridColumn = GridView1.Columns("Change")
    If column Is Nothing Then
        Return
    End If
    column.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
    If usePercentageFormat Then
        column.Caption = "Change <a href=toggle-display-mode>(%)</a>"
        column.DisplayFormat.FormatString = "p2"
        ' Display values from the PercentageChange field.
        column.UnboundExpression = "PercentageChange"
    Else
        column.Caption = "Change <a href=toggle-display-mode>($)</a>"
        column.DisplayFormat.FormatString = "c2"
        ' Display values from the AbsoluteChange field.
        column.UnboundExpression = "AbsoluteChange"
    End If
End Sub

Public Class Ticker
    Public Property Symbol() As String
    Public Property Price() As Decimal
    Public Property AbsoluteChange() As Decimal
    Public ReadOnly Property PercentageChange() As Decimal
        Get
            If Price > 0 Then
                Return AbsoluteChange / Price
            Else
                Return 0
            End If
        End Get
    End Property
End Class

See Also

AllowHtmlDrawHeaders

GridColumn.Caption

GridBand.Caption

HyperlinkClickMode

BandedGridView.HyperlinkClick

HTML-inspired Text Formatting

GridView Class

GridView Members

DevExpress.XtraGrid.Views.Grid Namespace