Back to Devexpress

ItemsView.CustomizeItem Event

windowsforms-devexpress-dot-xtragrid-dot-views-dot-items-dot-itemsview-cfb3129a.md

latest4.2 KB
Original Source

ItemsView.CustomizeItem Event

Allows you to customize individual items.

Namespace : DevExpress.XtraGrid.Views.Items

Assembly : DevExpress.XtraGrid.v25.2.dll

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

Declaration

csharp
[DXCategory("Events")]
public event CustomizeItemEventHandler CustomizeItem
vb
<DXCategory("Events")>
Public Event CustomizeItem As CustomizeItemEventHandler

Event Data

The CustomizeItem event's data class is DevExpress.XtraGrid.Views.Items.CustomizeItemArgs.

Remarks

You can handle the ItemCustomize event to dynamically modify individual items in the ItemsView. The ItemCustomize event fires repeatedly for each visible item.

Use the e.Element event parameter to access HTML elements of the currently processed item. The following methods allow you to retrieve HTML elements by tag, class, and ID:

  • Element.FindElementsByTag — Returns a list of HTML elements that have the specified tag.
  • Element.FindElementsByClass — Returns a list of HTML elements that are of the specified class.
  • Element.FindElementById — Returns an HTML element with the specified ID.

The elements returned by these methods expose properties to change element display settings. The main properties include:

  • HtmlElement.Hidden — Allows you to hide (collapse) the element.
  • HtmlElement.Disabled — Allows you to disable the element.
  • HtmlElement.Style — Allows you to modify CSS style properties applied to the element. This object exposes the SetBackgroundColor, SetForeColor, SetVisibility (displays an empty region instead of the element), and SetProperty methods for this purpose.

The following example changes visibility of HTML elements according to custom logic.

You can find the complete code of this sample in the following demo: Chat Client.

csharp
//CustomizeItem event handler:
void OnCustomizeItem(object sender, CustomizeItemArgs e) {
    //...
    if(message.IsLiked) {
        var btnLike = e.Element.FindElementById("btnLike");
        var btnMore = e.Element.FindElementById("btnMore");
        if(btnLike != null && btnMore != null) {
            btnLike.Hidden = false;
            btnMore.Hidden = true;
        }
    }
    if(message.IsFirstMessageOfBlock)
        return;
    if(!message.IsOwnMessage) {
        var avatar = e.Element.FindElementById("avatar");
        if(avatar != null)
            //Display an empty region instead of the 'avatar' element.
            avatar.Style.SetVisibility(Utils.Html.Internal.CssVisibility.Hidden);
    }
//...
}
vb
Private Sub OnCustomizeItem(ByVal sender As Object, ByVal e As CustomizeItemArgs)
    Dim message = TryCast(e.Row, DevAV.Chat.Model.Message)
    If message Is Nothing Then Return
    If message.IsLiked Then
        Dim btnLike = e.Element.FindElementById("btnLike")
        Dim btnMore = e.Element.FindElementById("btnMore")
        If btnLike IsNot Nothing AndAlso btnMore IsNot Nothing Then
            btnLike.Hidden = False
            btnMore.Hidden = True
        End If
    End If

    If message.IsFirstMessageOfBlock Then Return
    If Not message.IsOwnMessage Then
        Dim avatar = e.Element.FindElementById("avatar")
        'Display an empty region instead of the 'avatar' element.
        If avatar IsNot Nothing Then avatar.Style.SetVisibility(Utils.Html.Internal.CssVisibility.Hidden)
    End If
    '...
End Sub

See Also

ItemsView Class

ItemsView Members

DevExpress.XtraGrid.Views.Items Namespace