Skip to main content
All docs
V24.2

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

ItemsView.QueryItemTemplate Event

Allows you to assign and customize templates for individual items.

Namespace: DevExpress.XtraGrid.Views.Items

Assembly: DevExpress.XtraGrid.v24.2.dll

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

#Declaration

[DXCategory("Events")]
public event QueryItemTemplateEventHandler QueryItemTemplate

#Event Data

The QueryItemTemplate event's data class is DevExpress.XtraGrid.Views.Items.QueryItemTemplateEventArgs.

#Remarks

The QueryItemTemplate event fires repeatedly for each item. The event allows you to override the default template (ItemsView.HtmlTemplate) for individual items. To do this, assign a custom HTML-CSS template (a DevExpress.Utils.Html.HtmlTemplate object) to the e.Template event parameter.

You can also use the following properties to modify the HTML and CSS markup of the current template:

  • e.Template.Template — Specifies HTML markup.
  • e.Template.Styles — Specifies CSS styles.

Tip

The ItemsView’s ItemsView.HtmlTemplates property allows you to create multiple HTML-CSS templates at design time. You can then access these templates in your QueryItemTemplate event handler, and assign them to items.

The following ItemsView.QueryItemTemplate event handler assigns different templates to different items based on an item’s type (IsOwnMessage setting).

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

void OnQueryItemTemplate(object sender, QueryItemTemplateEventArgs e) {
    var message = e.Row as DevAV.Chat.Model.Message;
    if(message == null)
        return;
    if(message.IsOwnMessage)
        Styles.MyMessage.Apply(e.Template);
    else
        Styles.Message.Apply(e.Template);
    //...
}
See Also