Accessing the current DataRow of the DataSource from ItemDataBound

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

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


    Nathan Sokalski Guest

  2. Similar Questions and Discussions

    1. Accessing DataRow Items by Name in Edit/Update/Delete Event
      Hi there. I have a small problem with DataGrid in ASP.NET & C#. In the ItemDataBound Event I can use the following code DataRowView drvSE =...
    2. Accessing data from C# code-behind in the ItemDataBound Event
      Hello: If I want to access a particular column of data that is bound to a datagrid from within the ItemDataBound Event, how would I do that? I...
    3. Accessing the current datasource row and column?
      How would i go about accessing a column in the currently binding row in a datagrids datasource the following code does not give me access to the...
    4. Accessing a datasource at a network location
      Julie, I believe you've run into a permissions problem. Your web application is most likely running under the local account IUSR_<machinename>....
    5. accessing current written record
      See @@IDENTITY and SCOPE_IDENTITY( ) in SQL Server Books Online. -- HTH, Vyas, MVP (SQL Server) http://vyaskn.tripod.com/ What hardware is...
  3. #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

  4. #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" <njsokalski@hotmail.com> wrote in message
    news:uQqDp8wvFHA.3548@tk2msftngp13.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]njsokalski@hotmail.com[/email]
    > [url]http://www.nathansokalski.com/[/url]
    >

    Ken Tucker [MVP] Guest

  5. #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]njsokalski@hotmail.com[/email]
    [url]http://www.nathansokalski.com/[/url]

    "Adam Knight" <adam@pertrain.com.au> wrote in message
    news:%23Gk6qHxvFHA.2212@TK2MSFTNGP15.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. #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]njsokalski@hotmail.com[/email]
    [url]http://www.nathansokalski.com/[/url]

    "Adam Knight" <adam@pertrain.com.au> wrote in message
    news:%23Gk6qHxvFHA.2212@TK2MSFTNGP15.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

  7. #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" <njsokalski@hotmail.com> wrote in message
    news:OE%23kloyvFHA.2728@TK2MSFTNGP14.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]njsokalski@hotmail.com[/email]
    > [url]http://www.nathansokalski.com/[/url]
    >
    > "Adam Knight" <adam@pertrain.com.au> wrote in message
    > news:%23Gk6qHxvFHA.2212@TK2MSFTNGP15.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

Posting Permissions

  • You may not post new threads
  • You may 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