Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Accessing the current DataRow of the DataSource from ItemDataBound

Reply
Thread Tools

Accessing the current DataRow of the DataSource from ItemDataBound

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      09-22-2005
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.
--
Nathan Sokalski

http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
Adam Knight
Guest
Posts: n/a
 
      09-22-2005
Use the DataGridItems DataItem method.
It Returns a reference to the source data row as a DataRowView object.

Hope it helps.
Adam


 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      09-22-2005
Hi,

http://www.windowsformsdatagridhelp....5-b1ec09f63071

Ken
----------
"Nathan Sokalski" <> wrote in message
news:...
>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.
> --
> Nathan Sokalski
>
> http://www.nathansokalski.com/
>



 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      09-22-2005
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.
--
Nathan Sokalski

http://www.nathansokalski.com/

"Adam Knight" <> wrote in message
news:%...
> Use the DataGridItems DataItem method.
> It Returns a reference to the source data row as a DataRowView object.
>
> Hope it helps.
> Adam
>
>



 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      09-22-2005
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.
--
Nathan Sokalski

http://www.nathansokalski.com/

"Adam Knight" <> wrote in message
news:%...
> Use the DataGridItems DataItem method.
> It Returns a reference to the source data row as a DataRowView object.
>
> Hope it helps.
> Adam
>
>



 
Reply With Quote
 
Elton Wang
Guest
Posts: n/a
 
      09-25-2005
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

"Nathan Sokalski" <> wrote in message
news:OE%...
> 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.
> --
> Nathan Sokalski
>
> http://www.nathansokalski.com/
>
> "Adam Knight" <> wrote in message
> news:%...
>> Use the DataGridItems DataItem method.
>> It Returns a reference to the source data row as a DataRowView object.
>>
>> Hope it helps.
>> Adam
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bind Repeater's DataRow to GridView's DataSource wardemon ASP .Net 0 11-30-2006 06:20 PM
how to get value of datarow column on ItemDataBound xzzy ASP .Net Datagrid Control 1 06-19-2006 07:17 AM
Accessing the current DataRow of the DataSource from ItemDataBound Nathan Sokalski ASP .Net 5 09-25-2005 02:10 PM
Accessing the current DataRow of the DataSource from ItemDataBound Nathan Sokalski ASP .Net Web Controls 5 09-25-2005 02:10 PM
how to access the other tables in datasource from ItemDataBound roni ASP .Net 0 08-05-2005 04:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57