This is a migrated thread and some comments may be shown as answers.

dataitem is null

14 Answers 1002 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Balamurali Venkatesan
Top achievements
Rank 1
Balamurali Venkatesan asked on 15 Dec 2008, 12:04 AM
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

14 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 15 Dec 2008, 04:31 AM
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.
0
Balamurali Venkatesan
Top achievements
Rank 1
answered on 15 Dec 2008, 02:35 PM
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
0
Balamurali Venkatesan
Top achievements
Rank 1
answered on 15 Dec 2008, 02:52 PM
Hi,
Any help on this would be greatly appreciated.
0
Vlad
Telerik team
answered on 15 Dec 2008, 03:09 PM
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.
0
Jiří
Top achievements
Rank 1
answered on 16 Mar 2009, 04:01 PM
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
0
Princy
Top achievements
Rank 2
answered on 17 Mar 2009, 06:11 AM
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
0
QScend
Top achievements
Rank 1
answered on 16 Jun 2009, 10:57 PM
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.
0
Iana Tsolova
Telerik team
answered on 17 Jun 2009, 08:33 AM
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.
0
Fabio Rocha de Pinho
Top achievements
Rank 1
answered on 24 Jul 2009, 09:02 AM
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 
0
Sebastian
Telerik team
answered on 24 Jul 2009, 10:25 AM
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.
0
Fabio Rocha de Pinho
Top achievements
Rank 1
answered on 27 Jul 2009, 08:22 AM
That was very helpful Sebastian. thanks a million.
0
Kristin
Top achievements
Rank 1
answered on 19 Aug 2011, 09:15 PM
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. 

0
Kasim
Top achievements
Rank 2
answered on 16 Oct 2019, 10:41 AM
None of the solutions suggested above worked.
0
Attila Antal
Telerik team
answered on 18 Oct 2019, 02:37 PM

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.
Tags
Grid
Asked by
Balamurali Venkatesan
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Balamurali Venkatesan
Top achievements
Rank 1
Vlad
Telerik team
Jiří
Top achievements
Rank 1
Princy
Top achievements
Rank 2
QScend
Top achievements
Rank 1
Iana Tsolova
Telerik team
Fabio Rocha de Pinho
Top achievements
Rank 1
Sebastian
Telerik team
Kristin
Top achievements
Rank 1
Kasim
Top achievements
Rank 2
Attila Antal
Telerik team
Share this question
or