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.
Asked
Active
Viewed 4,857 times
2 Answers
4
Try this
DataRowView dr = (DataRowView)DataBinder.GetDataItem(e.Item);
using dr.Item.ItemArray you can access the entire row.

Gautam Sheth
- 2,442
- 1
- 17
- 16

Flatlineato
- 1,066
- 15
- 32
-
They must have changed this since whatever version of .NET you're using. There does not appear to be an .Item.ItemArray anymore. You can get at the columns by dr.Row["ColumnName"] though. – Timothy Lee Russell Dec 07 '12 at 18:36
1
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");
// ...
}
}

Jakkwylde
- 1,304
- 9
- 16
-
Close, but as far as I can tell that only gets me access to the one data item - the item that is being bound at that specific moment, and not the entire data row that is being bound in that iteration of the ListView ItemTemplate. – Nov 12 '09 at 22:35
-
Sorry, do you mean only one field for a given row or do you need to reference data from another row? – Jakkwylde Nov 12 '09 at 23:20
-
I think you meant Eval(dataItem.DataItem, "ProductType"); Then this works – clamchoda Mar 06 '18 at 20:39