Professional Web Applications Themes

Accessing the current DataRow of the DataSource from ItemDataBound - ASP.NET Data Grid Control

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 [email]njsokalskihotmail.com[/email] [url]http://www.nathansokalski.com/[/url]...

  1. #1

    Default Accessing the current DataRow of the DataSource from ItemDataBound

    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
    [email]njsokalskihotmail.com[/email]
    [url]http://www.nathansokalski.com/[/url]


    Nathan Sokalski Guest

  2. #2

    Default Re: Accessing the current DataRow of the DataSource from ItemDataBound

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

    Hope it helps.
    Adam


    Adam Knight Guest

  3. #3

    Default Re: Accessing the current DataRow of the DataSource from ItemDataBound

    Hi,

    [url]http://www.windowsformsdatagridhelp.com/default.aspx?ID=3785fb7f-69ce-4089-8a35-b1ec09f63071[/url]

    Ken
    ----------
    "Nathan Sokalski" <njsokalskihotmail.com> wrote in message
    news:uQqDp8wvFHA.3548tk2msftngp13.phx.gbl...
    >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
    > [email]njsokalskihotmail.com[/email]
    > [url]http://www.nathansokalski.com/[/url]
    >

    Ken Tucker [MVP] Guest

  4. #4

    Default Re: Accessing the current DataRow of the DataSource from ItemDataBound

    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
    [email]njsokalskihotmail.com[/email]
    [url]http://www.nathansokalski.com/[/url]

    "Adam Knight" <adampertrain.com.au> wrote in message
    news:%23Gk6qHxvFHA.2212TK2MSFTNGP15.phx.gbl...
    > Use the DataGridItems DataItem method.
    > It Returns a reference to the source data row as a DataRowView object.
    >
    > Hope it helps.
    > Adam
    >
    >

    Nathan Sokalski Guest

  5. #5

    Default Re: Accessing the current DataRow of the DataSource from ItemDataBound

    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
    [email]njsokalskihotmail.com[/email]
    [url]http://www.nathansokalski.com/[/url]

    "Adam Knight" <adampertrain.com.au> wrote in message
    news:%23Gk6qHxvFHA.2212TK2MSFTNGP15.phx.gbl...
    > Use the DataGridItems DataItem method.
    > It Returns a reference to the source data row as a DataRowView object.
    >
    > Hope it helps.
    > Adam
    >
    >

    Nathan Sokalski Guest

  6. #6

    Default Re: Accessing the current DataRow of the DataSource from ItemDataBound

    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" <njsokalskihotmail.com> wrote in message
    news:OE%23kloyvFHA.2728TK2MSFTNGP14.phx.gbl...
    > 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
    > [email]njsokalskihotmail.com[/email]
    > [url]http://www.nathansokalski.com/[/url]
    >
    > "Adam Knight" <adampertrain.com.au> wrote in message
    > news:%23Gk6qHxvFHA.2212TK2MSFTNGP15.phx.gbl...
    >> Use the DataGridItems DataItem method.
    >> It Returns a reference to the source data row as a DataRowView object.
    >>
    >> Hope it helps.
    >> Adam
    >>
    >>
    >
    >

    Elton Wang Guest

Similar Threads

  1. Accessing DataRow Items by Name in Edit/Update/Delete Event
    By Marc Eggenberger in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: January 31st, 02:07 PM
  2. Accessing data from C# code-behind in the ItemDataBound Event
    By hchattaway@bugcentral.com in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: January 7th, 03:33 PM
  3. Accessing the current datasource row and column?
    By Christian in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: June 2nd, 08:33 AM
  4. Accessing a datasource at a network location
    By Tim in forum ASP Components
    Replies: 0
    Last Post: July 1st, 01:52 AM
  5. accessing current written record
    By Narayana Vyas Kondreddi in forum Microsoft SQL / MS SQL Server
    Replies: 2
    Last Post: June 30th, 07:31 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139