Accessing the current DataRow of the DataSource from ItemDataBound

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.
 
A

Adam Knight

Use the DataGridItems DataItem method.
It Returns a reference to the source data row as a DataRowView object.

Hope it helps.
Adam
 
N

Nathan Sokalski

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.
 
N

Nathan Sokalski

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.
 
E

Elton Wang

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top