Results 1 to 12 of 12

Thread: DataGrid_ItemDataBound

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    DataGrid_ItemDataBound

    I have the following code.
    VB Code:
    1. Public Sub GetData()
    2.         Dim dt As DataTable = 'code to create datatable.
    3.  
    4.         gdSearch.DataSource = dt
    5.         gdSearch.DataBind()
    6. End Sub
    7.  
    8. Private Sub gdSearch_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles gdSearch.ItemDataBound
    9.         Dim DataItem As DataGridItem
    10.         If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    11.             DataItem = CType(e.Item, DataGridItem)
    12.            
    13.             'Code to populate row
    14.    
    15.         End If
    16. End Sub
    On the item bound how can I get hold of the DataRow that is used to populate this DataGridItem?

    Woof

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: DataGrid_ItemDataBound

    I don't think there is a direct way of doing it as the DataGridItem is only going to store the data it needs and not necessarily the entire DataRow - depends how you've got the DataGrid setup.

    Try setting up a DataKey for the DataGrid that is unique for each record and then you can grab this value in gdSearch_ItemDataBound and lookup the unique value in the DataTable to get the DataRow.

    Hope that makes sense.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: DataGrid_ItemDataBound

    Cheers. I have found a method, which works, and was just about to post it when u replied.
    I came up with:
    VB Code:
    1. Private Sub gdSearch_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles gdSearch.ItemDataBound
    2.         If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    3.             Dim DataItem As DataGridItem = DirectCast(e.Item, DataGridItem)
    4.             Dim dr As DataRowView = DirectCast(DataItem.DataItem, DataRowView)
    5.             Dim AssetKey As Integer = DirectCast(dr.Row.Item("AssetKey"), Integer)
    6.             Dim DateFrom As Date = DirectCast(dr.Row.Item("DateFrom"), Date)
    7.             Dim DateTo As Date = DirectCast(dr.Row.Item("DateTo"), Date)
    8.  
    9.             Dim Text As String = DateFrom.ToString("ddd dd MMM yyyy HH:mm") & " - " & DateTo.ToString("HH:mm")
    10.             Dim URL As String = "Javascript:openEditDialog("
    11.  
    12.             URL &= AssetKey & ", 0"
    13.             URL &= ", " & ((CType(DateFrom.ToString("HH"), Integer) * 60 + CType(DateFrom.ToString("mm"), Integer)) / 15).ToString
    14.             URL &= ", " & ((CType(DateTo.ToString("HH"), Integer) * 60 + CType(DateTo.ToString("mm"), Integer)) / 15).ToString
    15.             URL &= ", '" & DateFrom.ToString("dd/MMM/yyyy") & "'"
    16.             URL &= ", 0)"
    17.  
    18.             Dim lnk As HyperLink = DirectCast(DataItem.FindControl("lnkDetails"), HyperLink)
    19.  
    20.             lnk.Text = Text
    21.             lnk.NavigateUrl = URL
    22.         End If
    23.     End Sub
    The main 2 lines refering to the thread subject are:
    VB Code:
    1. Dim DataItem As DataGridItem = DirectCast(e.Item, DataGridItem)
    2. Dim dr As DataRowView = DirectCast(DataItem.DataItem, DataRowView)
    What do you think?

    Woka

  4. #4

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: DataGrid_ItemDataBound

    I have one other quick q. though.
    In the status bar in IE is shows "JavascriptpenEditDialog(Blah blah blah)"
    How can I change this to "Create Booking" what is the property of the hyper link control that needs to be changed?

    Woof

  5. #5
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: DataGrid_ItemDataBound

    Well you learn a new thing everyday! *Stores in recesses of mind*

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  6. #6
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    Re: DataGrid_ItemDataBound

    How can I change this to "Create Booking" what is the property of the hyper link control that needs to be changed?
    Add the following javascript in <a href tag

    onmouseover="javascript:window.status='Create Booking';"

  7. #7

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: DataGrid_ItemDataBound

    hi, that doesn't seem to work
    Whe I leave the hyperlinks then the status shows "Create Booking", but when I roll my mouse over the links then the status windows changes to Javascript: balh blah blah.

    Woof

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: DataGrid_ItemDataBound

    You got the JS working now?

  9. #9

  10. #10
    New Member leparduk's Avatar
    Join Date
    Oct 2005
    Posts
    7

    Re: DataGrid_ItemDataBound

    For the status script to work you should put your javascript in the onclick attribute for the href and leave the url as "#". Also use onmouseout to reset the status back to Empty string.
    Just because you're paranoid doesn't mean they aren't out to get you

    Quidquid latine dictum sit, altum viditur

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: DataGrid_ItemDataBound

    Going over this thread, there was a simpler method to get the datarow in the ItemDataBound. Access the row number in your table (in your dataset) using e.Item.Itemindex.

  12. #12

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: DataGrid_ItemDataBound

    Yea, but I don't have the datatable. I am not storing it as a modular level varible.
    I set the datasource, the GC deals with the temp dt I created.


    Woka

Posting Permissions

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



Click Here to Expand Forum to Full Width