Accessing the current DataRow of the DataSource from ItemDataBound

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

I want to access the DataRow used in DataBinding from the ItemDataBound
event. In my case, the reason for doing this is to determine whether I need
to make a word singular or plural. How can I do this? Thanks.
 
Use the DataGridItems DataItem method.
It Returns a reference to the source data row as a DataRowView object.

Hope it helps.
Adam
 
This seems to work except for one thing. I get the error "Option Strict On
disallows late binding." I can obviously turn Option Strict Off, but if
possible I would like to avoid doing this. Is this possible? Thanks.
 
Could you give me an example? I am using the following code:

Private Sub datRatings_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
datRatings.ItemDataBound

If CInt(CType(e.Item.DataItem, DataRowView)("timesrated")) > 1 Then

If e.Item.ItemType = ListItemType.Item Then
CType(e.Item.FindControl("lblTimesRated1"), Label).Text &= "s"

If e.Item.ItemType = ListItemType.AlternatingItem Then
CType(e.Item.FindControl("lblTimesRated2"), Label).Text &= "s"

End If

End Sub


And am recieving the following error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 140:
Line 141: Private Sub datRatings_ItemDataBound(ByVal sender As
Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles
datRatings.ItemDataBound
Line 142: If CInt(CType(e.Item.DataItem,
DataRowView)("timesrated")) > 1 Then
Line 143: If e.Item.ItemType = ListItemType.Item Then
CType(e.Item.FindControl("lblTimesRated1"), Label).Text &= "s"
Line 144: If e.Item.ItemType = ListItemType.AlternatingItem
Then CType(e.Item.FindControl("lblTimesRated2"), Label).Text &= "s"

Source File: C:\Inetpub\wwwroot\poetry\poemratings.aspx.vb Line: 142


Thanks.
 
Limiting your logic in
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem
' Process
End If

Otherwise, you might get Null Object reference error.

HTH
 
Back
Top