Back to Devexpress

ColumnView.CustomColumnDisplayText Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-base-dot-columnview-9b3b1feb.md

latest17.1 KB
Original Source

ColumnView.CustomColumnDisplayText Event

Allows you to replace cell display text (for cells with textbox-based editors), group row text, and filter menu item captions.

Namespace : DevExpress.XtraGrid.Views.Base

Assembly : DevExpress.XtraGrid.v25.2.dll

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

Declaration

csharp
[DXCategory("Data")]
public event CustomColumnDisplayTextEventHandler CustomColumnDisplayText
vb
<DXCategory("Data")>
Public Event CustomColumnDisplayText As CustomColumnDisplayTextEventHandler

Event Data

The CustomColumnDisplayText event's data class is CustomColumnDisplayTextEventArgs. The following properties provide information specific to this event:

PropertyDescription
ColumnGets the column that contains the cell currently being processed.
DisplayTextGets or sets the display text for the cell currently being processed.
GroupRowHandleGets the handle of the currently processed group row. Returns the GridControl.InvalidRowHandle value if the CustomColumnDisplayText event is not called for a group row.
IsForFilterGets a value that specifies whether the CustomColumnDisplayText event is raised for the Filter Panel.
IsForGroupRowReturns whether the CustomColumnDisplayText event is called to get the display text of a column value displayed within a group row.
ListSourceRowIndexGets the index in the data source of the row that contains the cell currently being processed.
ValueGets the edit value of the cell currently being processed.

Remarks

To modify the displayed text, change the e.DisplayText parameter value. The following code illustrates how to add units to the “Distance” cell values. You can find more examples at the end of this topic.

csharp
private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e) {
    if (e.Column.FieldName == "Distance") {
        e.DisplayText = ((int)e.Value).ToString() + " mil.";
    }
}
vb
Imports System

Private Sub gridView1_CustomColumnDisplayText(ByVal sender As Object, ByVal e As CustomColumnDisplayTextEventArgs)
    If e.Column.FieldName = "Distance" Then
        e.DisplayText = CInt(Math.Truncate(e.Value)).ToString() & " mil."
    End If
End Sub

To obtain the currently processed row, use the e.ListSourceRowIndex property. Note that it returns the same value for both Auto Filter and New Item rows. If you need to show custom strings in the cells of these rows, use the GridView.CustomDrawCell event instead.

csharp
private void Gv_CustomDrawCell(object sender, XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
    if (e.RowHandle == GridControl.AutoFilterRowHandle)
        e.DisplayText = "AutoFilterRow";
    if (e.RowHandle == GridControl.NewItemRowHandle)
        e.DisplayText = "NewItemRow";
}
vb
Private Sub Gv_CustomDrawCell(ByVal sender As Object, ByVal e As XtraGrid.Views.Base.RowCellCustomDrawEventArgs)
    If e.RowHandle = GridControl.AutoFilterRowHandle Then
        e.DisplayText = "AutoFilterRow"
    End If
    If e.RowHandle = GridControl.NewItemRowHandle Then
        e.DisplayText = "NewItemRow"
    End If
End Sub

Modify group row captions

Use the e.IsForGroupRow event parameter to identify whether the event was raised for a group row. The following code snippet handles the CustomColumnDisplayText event to add the item counter to Kanban Board group captions.

csharp
tileView1.CustomColumnDisplayText += TileView1_CustomColumnDisplayText;

private void TileView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e) {
    if (e.IsForGroupRow) {
        e.DisplayText = string.Format("{0} ({1})", e.DisplayText, CalcGroupCount(e.GroupRowHandle));
    }
}

int CalcGroupCount(int groupRowHandle) {
    int groupIndex = Math.Abs(groupRowHandle) - 1;
    return tileView1.DataController.GroupInfo[groupIndex].ChildControllerRowCount;
}
vb
AddHandler tileView1.CustomColumnDisplayText, AddressOf TileView1_CustomColumnDisplayText

Private Sub TileView1_CustomColumnDisplayText(ByVal sender As Object, ByVal e As CustomColumnDisplayTextEventArgs)
    If e.IsForGroupRow Then
        e.DisplayText = String.Format("{0} ({1})", e.DisplayText, CalcGroupCount(e.GroupRowHandle))
    End If
End Sub

Private Function CalcGroupCount(ByVal groupRowHandle As Integer) As Integer
    Dim groupIndex As Integer = Math.Abs(groupRowHandle) - 1
    Return tileView1.DataController.GroupInfo(groupIndex).ChildControllerRowCount
End Function

Modify filter menu item captions

If the event’s ListSourceRowIndex parameter returns the value of the static GridControl.InvalidRowHandle field, the event was raised for a filter menu item. Use the e.DisplayText property to rename filter items in drop-down filter menus.

csharp
private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) {
    if (e.Column.FieldName == "Distance" && e.ListSourceRowIndex == GridControl.InvalidRowHandle) {
        //change the e.DisplayText here
    }
}
vb
Private Sub gridView1_CustomColumnDisplayText(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs)
    If e.Column.FieldName = "Distance" AndAlso e.ListSourceRowIndex = GridControl.InvalidRowHandle Then
        'change the e.DisplayText here
    End If
End Sub

Handle the ColumnView.FilterPopupExcelData event to customize filter items in Excel style pop-up filter menus (the ColumnViewOptionsFilter.ColumnFilterPopupMode property).

Important notes

  • When you change cell display text, underlying values remain unchanged. Data Grid uses these data-layer values to filter and sort records. For that reason, if you handle the CustomColumnDisplayText event, you may also want to switch the GridColumn.SortMode and GridColumn.FilterMode properties to DisplayText. Note that in this case, the Data Grid will work with string values instead of actual data source objects.

  • Instead of replacing cell display text in the CustomColumnDisplayText event handler, you can create an unbound column and handle the ColumnView.CustomUnboundColumnData event to supply this column with values. Often, this is a better option since it does not affect Grid filters, and does not force you to change SortMode and FilterMode properties.

  • The CustomColumnDisplayText event does not allow you to display or modify text within cells that contain non-textbox-based editors (for example, PictureEdit, ImageEdit, CheckEdit, RadioGroup, ToggleSwitch, SparklineEdit).

Example 2

The following example demonstrates how to handle the ColumnView.CustomColumnDisplayText event to display custom display text in data cells. In the example empty strings are displayed within the “Discount” column’s cells if they contain zero values.

The result is shown below:

csharp
using DevExpress.XtraGrid.Views.Base;

private void gridView1_CustomColumnDisplayText(object sender, 
CustomColumnDisplayTextEventArgs e) {
   if(e.Column.FieldName == "Discount")
      if(Convert.ToDecimal(e.Value) == 0) e.DisplayText = "";
}
vb
Imports DevExpress.XtraGrid.Views.Base

Private Sub GridView1_CustomColumnDisplayText(ByVal sender As Object, _
ByVal e As CustomColumnDisplayTextEventArgs) Handles GridView1.CustomColumnDisplayText
   If e.Column.FieldName = "Discount" Then
      If e.Value = 0 Then e.DisplayText = ""
   End If
End Sub

Example 3

The following code snippet handles the CustomColumnDisplayText event to format prices based on a currency type column.

The currency type column contains only 0 and 1 values. If the currency type equals 0 , the price is displayed in US dollars. If the currency type equals 1 , the price is displayed in euros.

csharp
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid;
using System.Globalization;

// Culture settings for currency formatting
CultureInfo ciUSA = new CultureInfo("en-US");
CultureInfo ciEUR = new CultureInfo("fr-FR", false);

private void gridView1_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e) {
    ColumnView view = sender as ColumnView;

    // Process only valid data rows and the Price column.
    if (e.Column.FieldName != "Price" || e.ListSourceRowIndex == GridControl.InvalidRowHandle)
        return;

    int currencyType = (int)view.GetListSourceRowCellValue(e.ListSourceRowIndex, "CurrencyType");

    decimal price = Convert.ToDecimal(e.Value);

    // Select format based on currency type.
    e.DisplayText = currencyType == 0
        ? string.Format(ciUSA, "{0:c}", price)
        : string.Format(ciEUR, "{0:c}", price);
}
vb
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid
Imports System.Globalization

' Culture settings for currency formatting
Private ciUSA As New CultureInfo("en-US")
Private ciEUR As New CultureInfo("fr-FR", False)

Private Sub gridView1_CustomColumnDisplayText(
    sender As Object,
    e As CustomColumnDisplayTextEventArgs)

    Dim view As ColumnView = TryCast(sender, ColumnView)

    ' Process only valid data rows and the Price column.
    If e.Column.FieldName <> "Price" _
        OrElse e.ListSourceRowIndex = GridControl.InvalidRowHandle Then
        Return
    End If

    ' Read the currency type from the current row.
    Dim currencyType As Integer = CInt(
        view.GetListSourceRowCellValue(e.ListSourceRowIndex, "CurrencyType"))

    ' Convert the original value to Decimal.
    Dim price As Decimal = Convert.ToDecimal(e.Value)

    ' Select the display format based on the currency type.
    If currencyType = 0 Then
        e.DisplayText = String.Format(ciUSA, "{0:c}", price)
    Else
        e.DisplayText = String.Format(ciEUR, "{0:c}", price)
    End If
End Sub

The following code snippets (auto-collected from DevExpress Examples) contain references to the CustomColumnDisplayText event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-grid-richedit-highlight-search-results/CS/E4422/Form1.cs#L18

csharp
gridView1.CustomDrawCell += new DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(OnCustomDrawCell);
gridView1.CustomColumnDisplayText += new DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler(OnCustomColumnDisplayText);
List<Invoice> invoices = CreateData();

winforms-dashboard-custom-items/CS/TutorialsCustomItems/CustomItems/SimpleTableItem.cs#L43

csharp
view.OptionsView.ShowGroupPanel = false;
view.CustomColumnDisplayText += View_CustomColumnDisplayText;
grid.MainView = view;

winforms-dashboard-trend-line-indicators/CS/WinformsIndicator/Program.cs#L48

csharp
view.OptionsView.ShowGroupPanel = false;
view.CustomColumnDisplayText += View_CustomColumnDisplayText;
grid.MainView = view;

winforms-grid-richedit-highlight-search-results/VB/E4422/Form1.vb#L17

vb
AddHandler gridView1.CustomDrawCell, New DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventHandler(AddressOf OnCustomDrawCell)
AddHandler gridView1.CustomColumnDisplayText, New DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler(AddressOf OnCustomColumnDisplayText)
Dim invoices As List(Of Invoice) = CreateData()

winforms-dashboard-custom-items/VB/TutorialsCustomItems/CustomItems/SimpleTableItem.vb#L46

vb
view.OptionsView.ShowGroupPanel = False
AddHandler view.CustomColumnDisplayText, AddressOf View_CustomColumnDisplayText
grid.MainView = view

winforms-dashboard-trend-line-indicators/VB/WinformsIndicator/Program.vb#L56

vb
view.OptionsView.ShowGroupPanel = False
AddHandler view.CustomColumnDisplayText, AddressOf View_CustomColumnDisplayText
grid.MainView = view

See Also

GridView.GroupFormat

GridColumn.DisplayFormat

GridColumn.GroupFormat

RepositoryItem.DisplayFormat

IsValidRowHandle(Int32)

Format Cell Values

ColumnView Class

ColumnView Members

DevExpress.XtraGrid.Views.Base Namespace