Accessing the current DataRow of the DataSource from ItemDataBound

Discussion in 'ASP .Net Datagrid Control' started by Nathan Sokalski, Sep 22, 2005.

  1. 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/
    Nathan Sokalski, Sep 22, 2005
    #1
    1. Advertising

  2. Nathan Sokalski

    Adam Knight Guest

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

    Hope it helps.
    Adam
    Adam Knight, Sep 22, 2005
    #2
    1. Advertising

  3. Ken Tucker [MVP], Sep 22, 2005
    #3
  4. 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
    >
    >
    Nathan Sokalski, Sep 22, 2005
    #4
  5. 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
    >
    >
    Nathan Sokalski, Sep 22, 2005
    #5
  6. Nathan Sokalski

    Elton Wang Guest

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

    >
    >
    Elton Wang, Sep 25, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. roni
    Replies:
    0
    Views:
    502
  2. Nathan Sokalski
    Replies:
    5
    Views:
    5,685
    Elton Wang
    Sep 25, 2005
  3. wardemon
    Replies:
    0
    Views:
    944
    wardemon
    Nov 30, 2006
  4. xzzy

    how to get value of datarow column on ItemDataBound

    xzzy, Jun 18, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    335
    Bhuva
    Jun 19, 2006
  5. Nathan Sokalski

    Accessing the current DataRow of the DataSource from ItemDataBound

    Nathan Sokalski, Sep 22, 2005, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    143
    Elton Wang
    Sep 25, 2005
Loading...

Share This Page