dataitem is null

15 posts, 0 answers
  1. Balamurali Venkatesan
    Balamurali Venkatesan avatar
    94 posts
    Member since:
    Jul 2012

    Posted 14 Dec 2008 Link to this post

    HI,
    Iam using Object data osurce to populate a RadGrid.
    On button click Iam trying to access the GridDataItem's dataitem property and cast it to object .
    But the dataitem is always null.
    Please let us know how can we retrieve the object type of  a selected row  from the RadGrid on a button click event.
    Thanks,
    Bala
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 14 Dec 2008 Link to this post

    Hi Balamurali,

    The code snippet below shows how to access  selected row of the RadGrid inside a buttonclick event.

    CS:
    protected void Button1_Click(object sender, EventArgs e) 
       foreach (GridDataItem item in RadGrid1.SelectedItems) 
       { 
           string strtxt = item["CustomerID"].Text.ToString();             
       }  

    Thanks,
    Shinu.
  3. Balamurali Venkatesan
    Balamurali Venkatesan avatar
    94 posts
    Member since:
    Jul 2012

    Posted 15 Dec 2008 Link to this post

    HI,

    Thanks for your reply.
    I see the answer which you have suggested would be good to retrieve the values from each cell.
    But I need to get the entire object .

    Assume we have product object bound to the Radgrid.

    How do I cast and extract the product object from the griddataitem like

    griddataitem.Dataitem as Product;

    Thanks
    Bala
  4. Balamurali Venkatesan
    Balamurali Venkatesan avatar
    94 posts
    Member since:
    Jul 2012

    Posted 15 Dec 2008 Link to this post

    Hi,
    Any help on this would be greatly appreciated.
  5. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 15 Dec 2008 Link to this post

    Hi Balamurali,

    DataItem is null after post-back (button click) - this object is available only during data-binding. You can get the key value from the grid item and pass it to your collection to find desired item.

    All the best,
    Vlad
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. Jiří
    Jiří avatar
    36 posts
    Member since:
    Mar 2009

    Posted 16 Mar 2009 Link to this post

    You can call RadGrid.DataBind() to fill DataItem property. In case you want to access all data it is usefull and straghtforward way.

    Is it bad idea to have DataBind method for a GridDataItem?

    Jiří Nouza
  7. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 17 Mar 2009 Link to this post

    Hi,

    One way would be to store the datatitem in an arraylist and access it in the button click event.

    CS:
     protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            
            if (e.Item is GridDataItem) 
            { 
                GridDataItem item = (GridDataItem)e.Item; 
               
                list.Add(((System.Data.DataRowView)(item.DataItem)).Row); 
            } 
        } 
     
     
       protected void Button1_Click(object sender, EventArgs e) 
        { 
            foreach (Object obj in list) 
            { 
            } 
        } 

    Hope this helps !!!

    Thanks,
    Princy
  8. Keith LeBeau
    Keith LeBeau avatar
    51 posts
    Member since:
    Feb 2005

    Posted 16 Jun 2009 Link to this post

    We are having the same problem as this thread has been dealing with. We bind our RadGrid to a DataTable and use templated columns with columns not autogenerated. We've managed to keep everything working even through postbacks and also respond to ItemCommand when clicked, however we can't seem to get any access to the underlying data for the row. We get the correct index, however .DataItem seems to be null as stated. It doesn't seem logical to have to store our datatable in memory separately to access it in the handler. Is there a way to have the RadGrid persist the DataSource (DataTable in our case) and have us manually clear it out when it's convenient for us (i.e. PreRender)? That would save having two copies of the data in memory at once.
  9. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 17 Jun 2009 Link to this post

    Hello Keith,

    Could you please specify how are you binding your grid, through the NeedDataSource event or using simple data-binding? And where are you trying to access the DataItem, is it on ItemDataBound or elsewhere?

    Greetings,
    Iana
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  10. Fabio Rocha de Pinho
    Fabio Rocha de Pinho avatar
    11 posts
    Member since:
    Dec 2008

    Posted 24 Jul 2009 Link to this post

    Hi,

    I'm also stuck on this issue. I have a RadGrid and I'm trying to get the dataItem handling the ItemCommand event. But yeah, the DetailTableDataBind is fired after this method. So during the itemCommand handler event, the dataItem is null.

    Any thoughts?

    Thanks

    Fabio 
  11. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 24 Jul 2009 Link to this post

    Hello Fabio,

    The DataItem object is not null only when used in data bound context (inside the ItemDataBound handler of the grid). The same is applicable for the MS GridView control.

    If you would like to get reference to the properties of the underlying field, consider extracting their values from the row cells as illustrated in these resources:

    http://demos.telerik.com/aspnet-ajax/grid/examples/programming/detailtabledatabind/defaultcs.aspx
    http://www.telerik.com/help/aspnet-ajax/grdaccessingcellsandrows.html

    Kind regards,
    Sebastian
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  12. Fabio Rocha de Pinho
    Fabio Rocha de Pinho avatar
    11 posts
    Member since:
    Dec 2008

    Posted 27 Jul 2009 Link to this post

    That was very helpful Sebastian. thanks a million.
  13. Kristin
    Kristin avatar
    2 posts
    Member since:
    Nov 2010

    Posted 19 Aug 2011 Link to this post

    I notice that the Page Load() event fires first (before button Click() or grid ItemCommand()). So in the Page Load I put:
    RadGrid1.Rebind();

    Note: If this does not work you may need to put an event handler for the NeedDataSource() event. 

  14. Kasim
    Kasim avatar
    20 posts
    Member since:
    Feb 2016

    Posted 16 Oct 2019 in reply to Kristin Link to this post

    None of the solutions suggested above worked.
  15. Attila Antal
    Admin
    Attila Antal avatar
    441 posts

    Posted 18 Oct 2019 Link to this post

    Hi Kasim,

    The DataItem could be null due to several reasons. One cause could be if using incorrect data binding technique. Also, the DataItem is available during certain events, such as the ItemDataBound event of the Grid. This will not be available before in the ItemCreated, Load or Init events. That is how the controls is designed.

    Could you please share the steps you are trying to achieve and getting the null reference to the DataItem?

    Kind Regards,
    Attila Antal
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top