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

Why cant I set the GridDataItem ID?

3 Answers 73 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 23 Apr 2008, 02:10 PM
Hi,

Simple question: why when I am allowed to set the GridDataItem ID in the ItemDataBound event of RadGrid, does it then get overwritten with a numeric ID specifying its position within the grid when it is rendered?  Is it possible to stop this behaviour and have my own ID?

Thanks.

if (e.Item is Telerik.Web.UI.GridDataItem)  
{  
    e.Item.ID = "Whatver";  
}  
 
 
 

3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 24 Apr 2008, 01:07 PM
Hi Jonathan,

The grid need the id for the rows formated in special way in order to create respective client-side row objects. If you provide a bit more info about your scenario I will gladly help you to achieve your goal.

Best wishes,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Jonathan
Top achievements
Rank 1
answered on 25 Apr 2008, 07:29 AM
Hi,

Thanks for the reply.  It seems a little strange that you are allowed to set the ID property, but then it gets overwritten internally.

My issue is that I sometimes need to select a specific item in the grid from another pane in thw window.  I have access to the ID of the item which I would like to give to the individual items in the grid, but not the index of the item I want to select.  I therefore would like to (in JavaScript) be able to locate the row in the grid by my ID (which happens to be a database GUID) and select it.  I had planned to do this by assigning each row in the grid my specific ID, but now I cannot do this because of this restriction within the grid.

The only workaround I can think of is to manually add an attribute to each row of the grid in the ItemDataBound event as follows:

e.Item.Attributes["rowid"] = "MyID";  
 

However, this means that in the JavaScript I have to loop through every item in the grid trying to find my item and select it, which is very inefficient:

var rows = $find( "Grid" ).get_masterTableView().get_element().rows;  
forvar i = 0; i < rows.length; i++ )  
{  
    if( rows[ i ].rowid == rowID )  
    {  
        row = GetGridTable().get_dataItems()[i].get_element();  
        break;  
    }  
}  
          
 
0
Rosen
Telerik team
answered on 30 Apr 2008, 01:15 PM
Hi Jonathan,

Unfortunately as stated by my colleague item's id are reserved for internal use and cannot be modified. Therefore you can use similar to proposed by you approach. I suggest you to use ClientDataKeyNames instead for setting your own attribute. Information on using ClientDataKeyNames can be found in online documentation at this location.

Sincerely yours,
Rosen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Jonathan
Top achievements
Rank 1
Rosen
Telerik team
Share this question
or