Прочитать на английском

Поделиться через


ListViewDataItem.DataItem Свойство

Определение

Получает или задает объект данных, к которому присоединен объект ListViewItem.

C#
public virtual object DataItem { get; set; }
C#
public override object DataItem { get; set; }

Значение свойства

Объект данных, к которому присоединен объект ListViewItem.

Примеры

В следующем примере показано, как использовать DataItem свойство для получения значения поля. Затем значение используется для предварительного выбора элемента в элементе DropDownList управления, который отображается, когда элемент находится в режиме редактирования. Этот пример кода является частью более широкого примера для класса ListViewDataItem.

C#
protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{

  //Verify there is an item being edited.
  if (ContactsListView.EditIndex >= 0)
  {

    //Get the item object.
    ListViewDataItem dataItem = (ListViewDataItem)e.Item;

    // Check for an item in edit mode.
    if (dataItem.DisplayIndex == ContactsListView.EditIndex)
    {

      // Preselect the DropDownList control with the Title value
      // for the current item.

      // Retrieve the underlying data item. In this example
      // the underlying data item is a DataRowView object.        
      DataRowView rowView = (DataRowView)dataItem.DataItem;

      // Retrieve the Title value for the current item. 
      String title = rowView["Title"].ToString();

      // Retrieve the DropDownList control from the current row. 
      DropDownList list = (DropDownList)dataItem.FindControl("TitlesList");

      // Find the ListItem object in the DropDownList control with the 
      // title value and select the item.
      ListItem item = list.Items.FindByText(title);
      list.SelectedIndex = list.Items.IndexOf(item);
                      
    }
  }
}

Комментарии

Свойство DataItem доступно только во время и после ItemDataBound события ListView элемента управления .

Применяется к

Продукт Версии
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

См. также раздел