e.Item.DataItem

Discussion in 'ASP .Net Datagrid Control' started by anonymous, Mar 2, 2005.

  1. anonymous

    anonymous Guest

    Hello,

    I need to pass e.item.dataitem to my user control,
    from the ItemCommand method. But each time I do this, I
    get the following error:

    Object variable or With block variable not set.

    It happens when I access the data. Its something like this:

    Private Sub Dg_ItemCommand...
    Dim drv As Object = e.Item.DataItem
    objUC.Binddata(drv(1), drv(2), "%", "%","%")
    End Sub

    Any idea?
     
    anonymous, Mar 2, 2005
    #1
    1. Advertisements

  2. anonymous

    Arthur Dent Guest

    make sure youre only passing the item if
    e.Item.ItemType = Item OrElse e.Item.ItemType = AlternatingItem.
    Those are the only types for which DataItem is valid. If you try to refer to
    it on a type Header, Pager, Footer, youll get that error.
     
    Arthur Dent, Mar 2, 2005
    #2
    1. Advertisements

  3. anonymous

    Scott M. Guest

    Which particular line is failing? Is it:

    Dim drv As Object = e.Item.DataItem

    or is it:

    objUC.Binddata(drv(1), drv(2), "%", "%","%")

    My guess is that it is the last one where you are trying to use drv as if it
    was an array when, in fact, it isn't.
     
    Scott M., Mar 2, 2005
    #3
  4. anonymous

    anonymous Guest

    The last line is failing. How I can resolve that?
     
    anonymous, Mar 2, 2005
    #4
  5. anonymous

    Anonymous Guest

    As said I am using the code in the ItemCommand method. I
    trued to put the code inside the item type, but still get
    the same error. The error occurs at this line:

    objUC.Binddata(drv(1), drv(2), "%", "%","%")
     
    Anonymous, Mar 2, 2005
    #5
  6. anonymous

    anonymous Guest

    According to the MSDN doc, I can use it this way:

    drv("CompanyName").ToString()

    I tried this, but get the same error. Any help is really
    really apprecaited.

    MSDN: http://msdn.microsoft.com/library/en-
    us/cpref/html/frlrfSystemDataDataRowViewClassItemTopic2.asp
    ?frame=true

    Thanks
     
    anonymous, Mar 2, 2005
    #6
  7. anonymous

    Scott M. Guest

    But headers and footers and pagers are items, so you must test
    e.Item.DataItem as Arthur suggests.
     
    Scott M., Mar 2, 2005
    #7
  8. anonymous

    Elton Wang Guest

    Hi there,

    Item.DataItem is from underlying data source of the
    datagrid. Mostly it is used in ItemCreated event and
    ItemDataBound events. Unless you re-bind datagrid's data
    source, when postback the datagrid loses its underlying
    data source.

    I suppose you can get data from the datagrid itself, like
    e.Item.Cells(colIndex).Text

    HTH

    Elton Wang
     
    Elton Wang, Mar 2, 2005
    #8
  9. anonymous

    Scott M. Guest

    Well, technically it's not the DataSource that is the problem. Because of
    ViewState, the grid will remember what its data source is, it just won't
    have bound itself to it unless you call DataBind.
     
    Scott M., Mar 2, 2005
    #9
  10. anonymous

    Elton Wang Guest

    That's what you think.
     
    Elton Wang, Mar 2, 2005
    #10
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.