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

[Solved] Accessing data source info for row at ItemDataBound

2 Answers 886 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carl
Top achievements
Rank 1
Carl asked on 30 Sep 2009, 07:16 PM

Towards making it easier to provide help, I would like to briefly describe what I want to do with Telerik Grid.

 

I am developing an application where the grid is used to display 4 rows of 5 columns of thumbnail images. Paging is enabled because there can be a thousand rows of data. There is a caption directly under each thumbnail. There is a hidden field that has the user ID associated with the thumbnail image.

 

Clicking on the thumbnail, invokes a client side event handler which gets the user ID for that image.  An Ajax call is made to the server with the user ID to get the url of a full size image, and various user data. The full size image and associated user information will be displayed on a separate tab at the end of the Ajax call by JavaScript on the client.

 

I am using a GridTemplateColumn to allow me to build the more involved grid cell that contains and image, caption, and hidden field HtmlControls with the runat=”server”  

I am assuming that I can either set the grid DataSource property and call the DataBind method or handle this in the NeedDataSource event.

 

I plan to use the ItemDataBound event to set the alt and url attribute of the <img>tag along with the caption text held in a <div> container. I will also need to set the value attribute of the hidden field to the user ID.

 

My data source will be a List<AlbumInfoItemCollection> that has four properties. Each property is a get accessor for an AlbumInfoItem. The AlbumInfoItem class has properties that hold all the information necessary to set the url, caption, and user ID.

 

Basic Problem/Question

My basic problem is when handling the ItemDataBound event, I need to know the syntax to get the row instance of the AlbumInfoItemCollection.

 

I would imagine that they do exist, but I could not find any examples that did this.

Thanks in advance,
Carl Taylor
Suwanee, GA

PS I absolutely love your product!!!

2 Answers, 1 is accepted

Sort by
0
Carl
Top achievements
Rank 1
answered on 01 Oct 2009, 04:48 AM
In reading over the Telerik documentation, I can't find where the control offers any access to the data source for a particular row.

It looks like the only way to do this is kind of indirectly.  I can set class field reference to the data source used in data binding, and then use e.Item.RowIndex to index into the data source to pull out the correct data for each cell thumbnail.

Since all the columns are of GridTemplateColumn type with a complex class instance holding multiple data info, the grid is really not doing any real data binding in the normal way we think of doing data binding.

I have three things to set for each grid cell: thumbnail url, caption, and user ID held in a hidden field. Considering this, it seems to make sense to set these values when handling the ItemCreated event.

Well that is all folks for now. Way ;past my bed time. If any of you want to jump in and expand the possibilities or provide correction to the concepts that I have put forth, please do. You won't hurt my feelings in the slightest.

Cheers,
Carl Taylor
Suwanee, GA USA
0
Rosen
Telerik team
answered on 06 Oct 2009, 07:37 AM
Hello Carl,

You can get a reference to the object to which the current RadGrid's GridDataItem is bound to through item's DataItem property. Similar to the following:

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridDataItem)  
        {  
            AlbumInfoItem item = (AlbumInfoItem)e.Item.DataItem;  
        }  
    } 

Please have in mind that this property is populated only when the RadGrid control is created from its associated data set and will not be available when RadGrid's control collection is recreated from the ViewState (on a postback from external control for example).

Kind regards,
Rosen
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Carl
Top achievements
Rank 1
Answers by
Carl
Top achievements
Rank 1
Rosen
Telerik team
Share this question
or