Back to Devexpress

ASPxGridView.CustomColumnDisplayText Event

aspnet-devexpress-dot-web-dot-aspxgridview-cadc1557.md

latest10.4 KB
Original Source

ASPxGridView.CustomColumnDisplayText Event

Enables you to specify the display text for a cell.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public event ASPxGridViewColumnDisplayTextEventHandler CustomColumnDisplayText
vb
Public Event CustomColumnDisplayText As ASPxGridViewColumnDisplayTextEventHandler

Event Data

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

PropertyDescription
ColumnGets the data column that contains the cell currently being processed.
DisplayTextEnables you to set a custom text for the cell currently being processed. Inherited from ASPxGridColumnDisplayTextEventArgs.
EncodeHtmlGets or sets a value that specifies whether the cell display text keeps any of its values that are HTML as HTML, or instead, strips out the HTML markers. Inherited from ASPxGridColumnDisplayTextEventArgs.
KindGets the type of operations with grid data. Inherited from ASPxGridColumnDisplayTextEventArgs.
ValueGets the edit value of the cell currently being processed. Inherited from ASPxGridColumnDisplayTextEventArgs.
VisibleIndexGets the visible index of the data item (row, card or record) where the processed cell resides. Inherited from ASPxGridColumnDisplayTextEventArgs.
VisibleRowIndexObsolete. Gets the visible index of the data row where the processed cell resides.

The event data class exposes the following methods:

MethodDescription
GetFieldValue(Int32, String)Returns the value of the specified data source field in the specified data item (row, card or record). Inherited from ASPxGridColumnDisplayTextEventArgs.
GetFieldValue(String)Returns the value of the specified data source field in the current data item (row, card or record). Inherited from ASPxGridColumnDisplayTextEventArgs.

Remarks

The CustomColumnDisplayText event occurs for bound and unbound columns. This event allows you to specify the text that is displayed in the grid.

Run Demo: Data Binding

The CustomColumnDisplayText event fires in the following cases:

  • While the control’s hierarchy is building.

  • When you sort or filter a grid by display text (the GridDataColumnSettings.FilterMode or the ASPxGridBehaviorSettings.SortMode property is set to DisplayText). In this case, the rows’ visible indexes are not taken into account and the GetFieldValue(int visibleRowIndex, string fieldName) method returns -1.

Limitations

  • The CustomColumnDisplayText event is not in effect for template columns.
  • The CustomColumnDisplayText events is not in effect for the GridViewDataCheckColumn column as the grid filters only the column values and ignores its display text. To specify a custom display text for this column, use the DisplayTextChecked and DisplayTextUnchecked properties.

Export Display Text

When exporting data in XLS or XLSX format, the ASPxGridView control exports cell values. If you need to export display text specified in the CustomColumnDisplayText event handler set the TextExportMode property of the XlsExportOptionsEx or XlsxExportOptionsEx object to Text.

Default Export Commands

aspx
<dx:ASPxGridView ID="grid" runat="server" DataSourceID="CustomerReportsDataSource"
    OnCustomColumnDisplayText="grid_CustomColumnDisplayText">
    <SettingsExport EnableClientSideExportAPI="true"/>
    <SettingsContextMenu Enabled="true">
        <RowMenuItemVisibility ExportMenu-Visible="true" />
    </SettingsContextMenu>
    <!--...-->
csharp
using DevExpress.Web;

protected void grid_CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e) {
    if (e.Column.FieldName == "ProductAmount" && Convert.ToDecimal(e.Value) == 0) {
        e.DisplayText = "none";
    }
}

protected void grid_BeforeExport(object sender, ASPxGridBeforeExportEventArgs e) {
    switch(e.ExportTarget) {
        case ExportTarget.Xls:
            XlsExportOptions optionsXls = e.ExportOptions as XlsExportOptions;
            optionsXls.TextExportMode = TextExportMode.Text;
            break;
        case ExportTarget.Xlsx:
            XlsxExportOptions optionsXlsx = e.ExportOptions as XlsxExportOptions;
            optionsXlsx.TextExportMode = TextExportMode.Text;
            break;
        default:
            break;
    }
}
vb
Imports DevExpress.Web

Protected Sub grid_CustomColumnDisplayText(ByVal sender As Object, ByVal e As ASPxGridViewColumnDisplayTextEventArgs)
    If e.Column.FieldName = "ProductAmount" AndAlso Convert.ToDecimal(e.Value) = 0 Then
        e.DisplayText = "none"
    End If
End Sub

Protected Sub grid_BeforeExport(ByVal sender As Object, ByVal e As ASPxGridBeforeExportEventArgs)
    Select Case e.ExportTarget
        Case ExportTarget.Xls
            Dim optionsXls As XlsExportOptions = TryCast(e.ExportOptions, XlsExportOptions)
            optionsXls.TextExportMode = TextExportMode.Text
        Case ExportTarget.Xlsx
            Dim optionsXlsx As XlsxExportOptions = TryCast(e.ExportOptions, XlsxExportOptions)
            optionsXlsx.TextExportMode = TextExportMode.Text
        Case Else
    End Select
End Sub

Export Methods

aspx
<dx:ASPxButton ID="ASPxButton1" runat="server" Text="Export to XLSX" OnClick="button_Click" />
<dx:ASPxGridView ID="grid" runat="server" DataSourceID="CustomerReportsDataSource"
    OnCustomColumnDisplayText="grid_CustomColumnDisplayText" />
csharp
using DevExpress.Web;

protected void grid_CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e) {
    if (e.Column.FieldName == "ProductAmount" && Convert.ToDecimal(e.Value) == 0) {
        e.DisplayText = "none";
    }
}

protected void button_Click(object sender, EventArgs e) {
    var exportOptions = new DevExpress.XtraPrinting.XlsxExportOptionsEx();
    exportOptions.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text;
    grid.ExportXlsxToResponse(exportOptions);
}
vb
Imports DevExpress.Web

Protected Sub grid_CustomColumnDisplayText(ByVal sender As Object, ByVal e As ASPxGridViewColumnDisplayTextEventArgs)
    If e.Column.FieldName = "ProductAmount" AndAlso Convert.ToDecimal(e.Value) = 0 Then
        e.DisplayText = "none"
    End If
End Sub

Protected Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim exportOptions = New DevExpress.XtraPrinting.XlsxExportOptionsEx()
    exportOptions.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text
    grid.ExportXlsxToResponse(exportOptions)
End Sub

Example

This example demonstrates how to display the “empty” string within the Units On Order column’s cells if they contain zero values.

The image below shows the result:

csharp
protected void ASPxGridView2_CustomColumnDisplayText(object sender,
    DevExpress.Web.ASPxGridViewColumnDisplayTextEventArgs e) {
    if (e.Column.FieldName != "UnitsOnOrder") return;
    if (Convert.ToInt32(e.Value) == 0)
        e.DisplayText = "empty";
}
vb
Protected Sub ASPxGridView1_CustomColumnDisplayText(ByVal sender As Object,_
    ByVal e As DevExpress.Web.ASPxGridViewColumnDisplayTextEventArgs)_
    Handles ASPxGridView1.CustomColumnDisplayText
    If e.Column.FieldName = "UnitsOnOrder" Then
        If Convert.ToInt32(e.Value) = 0 Then
            e.DisplayText = "empty"
        End If
    End If
End Sub

See Also

Format Data

Grid View

ASPxGridView Class

ASPxGridView Members

DevExpress.Web Namespace