How to cast object from (object sender, ListViewItemEventArgs e)

asp.netlistview

I am using the ListView Control with the following datasource List<MyObject>

On my listview control i have an OnItemDataBound

My question is how do get the current value of MyObject. Ie myObj[5].FirstName

protected void ItemsListViewDataBound(object sender, ListViewItemEventArgs e)
{
// I want to do some kind of a cast here

}

Best Answer

protected void MyListView_DataBind(object sender, ListViewItemEventArgs e){
  if(e.Item.ItemType == ListViewItemType.DataItem){
    MyObject p = (MyObject)((ListViewDataItem)e.Item).DataItem;
  }
}

You'll want to do the type check so that you don't try and do a cast when you're working on say, the header item.