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

Can't access dataitem

8 Answers 739 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rob Ellis
Top achievements
Rank 1
Rob Ellis asked on 10 Apr 2008, 05:03 AM
I'm binding the Prometheus RadGrid with a generic List of customer business objects.  I need to access the business object that is associated with that row.  Normally I would do this with e.item.dataitem but this is exclusively null in the RadGrid which is diferent to all of Microsoft's controls.

Could you tell me the best way to access it?  I've read up on Cells using uniquenames etc, but can't make it work.

Thanks,

Rob

8 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 10 Apr 2008, 07:57 AM
Hi Rob,

e.Item.DataItem will be not null only during data-binding - ItemDataBound event in both RadGrid and Microsoft GridView and DataGrid. Only if you bind the grid on every post-back you will have e.Item.DataItem (again for both MS and our grid).

You can use DataKeyNames to set desired key name and access key value in post back events. Here is an example for ItemCommand:

if(e.CommandName == "Delete")
{
    object value = ((GridDataItem)e.Item).GetDataKeyValue("MyKeyName");
}

Best wishes,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Rob Ellis
Top achievements
Rank 1
answered on 11 Apr 2008, 07:12 AM
Hi Vlad,

Thanks for the response.  Doesn't GetDataKeyValue just return the bound id?  If I've already bound a list of objects to the RadGrid I don't want to go back to the database again.  I can't seem to make the DataKey bind to an object (such as my business object).

The crucial difference between the Microsoft controls and Telerik control is that the RadGrid doesn't bind the DataItem even on the original DataBind, whereas with the Microsoft controls I can just cast the DataItem directly into my business object.

Thanks again,

Rob
0
Vlad
Telerik team
answered on 11 Apr 2008, 07:57 AM
Hello Rob,

RadGrid will return the key in similar to MS DataGrid and MS GridView way. I'm not sure what do you mean by

" The crucial difference between the Microsoft controls and Telerik control is that the RadGrid doesn't bind the DataItem even on the original DataBind, whereas with the Microsoft controls I can just cast the DataItem directly into my business object."

you can cast e.Item.DataItem to your object in exactly the same way as in standard grids.

You can check attached example.

Regards,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Andy
Top achievements
Rank 2
answered on 30 Apr 2008, 09:36 AM
I'm having the same issue.

In the ItemCommand event e.Item.DataItem is null.

I want to cast this object to my business object.

The example you provide is for RowDataBound and ItemDataBound, not ItemCommand.

Would you be able to provide an example of how to access the DataItem and cast it to a business object in the ItemCommand event?

Thanks

Andy
0
Bodevain Svensson
Top achievements
Rank 1
answered on 05 May 2008, 08:59 AM
If I remember well I read somewhere that the DataItem object is available only in data-bound context (inside the RowDataBound/ItemDataBound handlers). If you want to fetch values from grid records from other event handlers, use the unique name of the column and the Text property of the cell to get them. See this topic which elaborates on the subject.

Bodevain
0
Kevin Robinson
Top achievements
Rank 1
answered on 30 Jul 2008, 03:56 PM
I don't think it's the event handler that is wrong, simply that, unlike the MS GridView, RadGrid splits one row into several component Items, each of them firing an "ItemDataBound" event.

So one "row" might fire several ItemDataBound Events, passing a different type of Item in the event args each time.

But some items in a row, like GridDataItems, have the .DataItem object passed through to them, and others, like GridItemCommands, don't.

This is understandable behaviour for headers, footers and pagers as there is no real "Data Row" powering those types of rows.

But a GridItemCommand is none of these things and, presumably, exists as an item in a row that is there to have, "Item Commands" in, commands that in all likelihood would need to know about the business object that powered that particular row.

So, is this for real? 

The ItemCommand "Item" generates an "ItemDataBound" event but does not provide access to the DataItem behind the data binding?

If we want to add something dynamic into the CommandItem, like say, an item that does a command, then we have to construct it *without* being able to cast the business object data for that row into a business object? All we can do is dig around table cells and convert string data, with no compile time checking or type safety? The words "Hoops" and "Jumping" spring to mind...

Plus there is NO opportunity to declaratively databind using the design tools in the GridItemCommand template as the DataItem is not passed so databinding in this template does not work?

One might think of "tracking" the business object for a row with a class member, for example, "_lastDataItem" so business objects from previous GridDataItems could be remembered when the time of the ItemCommand (for that row) came.

But realising that setting the CommandItemDisplay to "Top" would render this hack very fragile indeed, implementing it would be a rather poor choice. But then it appears that the options suggested thus far are not really any better.

So what to do?

Please, please confirm this is all an awful misunderstanding on my part and is not really the case at all.

0
Vladimir
Top achievements
Rank 1
answered on 30 Jul 2008, 07:03 PM
Kevin,

You cannot access DataItem in ItemCommand exactly in the same way as in standard MS GridView and DataGrid. The only option is to use DataKeyNames/DataKeyValues to retrieve the key field value and use this to find the item in your collection.

Vlad
0
Kevin Robinson
Top achievements
Rank 1
answered on 30 Jul 2008, 09:42 PM
Hi Vlad,

I'm very glad to say that I realise my understanding of how this was working was not at all correct.

In addition, the fact that hierarchy data is loaded "as requested" meant that what looked like it should work, in fact didn't, because the databinding for each level was happening on different postbacks (so DataItem for the parent table row was now null, having already been bound during the previous postback).

Many thanks

Kevin










Tags
Grid
Asked by
Rob Ellis
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Rob Ellis
Top achievements
Rank 1
Andy
Top achievements
Rank 2
Bodevain Svensson
Top achievements
Rank 1
Kevin Robinson
Top achievements
Rank 1
Vladimir
Top achievements
Rank 1
Share this question
or