ListView - ItemDataBound Event

Other usage samples: Simple List View | ModelBinding Sample | Grouping Sample | Layout Test | ItemDataBound Event | CRUD Operations

This example demonstrates using the OnItemDataBound event to access ListViewItem and ListViewDataItem properties.

Id Name Price
1 First Widget ¤7.99
2 Second Widget ¤13.99
3 Third Widget ¤100.99
4 Fourth Widget ¤10.99
5 Fifth Widget ¤5.99
6 Sixth Widget ¤6.99
7 Seventh Widget ¤12.99
8 Eighth Widget ¤8.99
9 Ninth Widget ¤2.99
10 Tenth Widget ¤3.99
11 Eleventh Widget ¤16.99
12 Fritz's Widget ¤52.70

ItemDataBound Event Details

The OnItemDataBound event fires for each item as it's rendered. The event provides access to:

  • ItemType: The type of item (DataItem, InsertItem, or EmptyItem)
  • DataItemIndex: The index of the data item in the underlying data source
  • DisplayIndex: The position of the item as displayed in the ListView
  • DataItem: The underlying data object bound to this item

This example shows 12 items, each with a ListViewDataItem object available in the OnItemDataBound event.


Code Example

<ListView Items="Widget.SimpleWidgetList"
          ItemType="Widget"
          OnItemDataBound="HandleItemDataBound"
          Context="Item">
    <ItemTemplate>
        <tr>
            <td>@Item.Name</td>
            <td>@Item.Price.ToString("c")</td>
        </tr>
    </ItemTemplate>
</ListView>

@code {
    private void HandleItemDataBound(ListViewItemEventArgs e)
    {
        if (e.Item is ListViewDataItem dataItem)
        {
            var widget = (Widget)dataItem.DataItem;
            // Access dataItem.DataItemIndex, dataItem.DisplayIndex
            // Access dataItem.ItemType (DataItem, InsertItem, EmptyItem)
        }
    }
}

Code Example

<ListView Items="Widget.SimpleWidgetList"
          ItemType="Widget"
          OnItemDataBound="HandleItemDataBound"
          Context="Item">
    <ItemTemplate>
        <tr>
            <td>@Item.Name</td>
            <td>@Item.Price.ToString("c")</td>
        </tr>
    </ItemTemplate>
</ListView>

@code {
    private void HandleItemDataBound(ListViewItemEventArgs e)
    {
        if (e.Item is ListViewDataItem dataItem)
        {
            var widget = (Widget)dataItem.DataItem;
            // Access dataItem.DataItemIndex, dataItem.DisplayIndex
            // Access dataItem.ItemType (DataItem, InsertItem, EmptyItem)
        }
    }
}