Accessing the full DataRow from the DataSource in a ListView ItemDataBound event handler

試著忘記壹切 提交于 2019-12-08 22:02:25

问题


Is it at all possible within a ListView ItemDataBound event handler to gain access to the full DataRow for that event? I need to do a lot of processing for the entire row on binding, but using data item values in the datarow that I am not actually using in the display itself.


回答1:


Try this

DataRowView dr = (DataRowView)DataBinder.GetDataItem(e.Item);

using dr.Item.ItemArray you can access the entire row.




回答2:


Perhaps try to use the ListViewDataItem property to access the properties of the underlying data object to which the object is bound. The ListViewDataItem property is only available during and after the ItemDataBound events of the control and usually corresponds to a record in your data source object.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewdataitem.aspx

Below is an example.

protected void listProducts_ItemDataBound(object sender, ListViewItemEventArgs e)
{
  if (e.Item.ItemType == ListViewItemType.DataItem)
  {
    ListViewDataItem dataItem = (ListViewDataItem)e.Item;
    string prodtype = (string)DataBinder.Eval(dataItem, "ProductType");
    // ...
  }
}


来源:https://stackoverflow.com/questions/1725230/accessing-the-full-datarow-from-the-datasource-in-a-listview-itemdatabound-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!