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

RadGrid KeyValue

6 Answers 358 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marcio Nascimento
Top achievements
Rank 1
Marcio Nascimento asked on 22 Aug 2009, 02:43 PM
Hi there,

In the moment that you insert a row on RadGrid, it is possible to obtain a keyvalue of that row ?

Thanks any help.

Marcio Nascimento

6 Answers, 1 is accepted

Sort by
0
ManniAT
Top achievements
Rank 2
answered on 22 Aug 2009, 03:34 PM
Hi Marcio,

there is an "ItemInserted" event for the RadGrid.
This raises AFTER the item is inserted. Here have access to the newly inserted values.

Regards

Manfred
PS: if the post was helpful please use "Mark as answer"
0
Marcio Nascimento
Top achievements
Rank 1
answered on 22 Aug 2009, 05:25 PM
Hi Manfred,

You're right. However, I can't access my KeyValue because my key is "Identity" on database, so I don't think RadGrid can access this value...or I'm wrong ?

Marcio Nascimento
0
ManniAT
Top achievements
Rank 2
answered on 22 Aug 2009, 05:53 PM
Hi,

if it is just to get the "NewIdentity" from your database - simply handle the event of your datasource.
It's all a matter of what you need when.
Of course (sooner or later) RadGrid will also get the new value from your database.
But (for an example) if you have to handle the things BEFORE the new data is bound to your Grid you should use the events of your dataprovider.

I'm not 100% sure WHEN the grid gets the new data - I guess when the mode changes from edit to normal the new data should be there.

Regards

Manfred
PS: if this post was helpful please use "Mark as answer"
0
Marcio Nascimento
Top achievements
Rank 1
answered on 22 Aug 2009, 10:38 PM
Hi Manfred,

I solved the problem using the code below:

    protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)  
    {  
        RadGrid1.DataBind();  
        GridItemCollection gridRows = RadGrid1.Items;  
        string LastKeyValue = string.Empty;  
        foreach (GridDataItem data in gridRows)  
        {  
            LastKeyValue = data.GetDataKeyValue("id_plan").ToString();  
        }  
        TextBox1.Text = LastKeyValue;  
    } 

However, I believe that should be a better way because when the Grid becomes bigger, the foreach gets slower. What do you think ?

Regards,

Marcio Nascimento
0
Accepted
ManniAT
Top achievements
Rank 2
answered on 23 Aug 2009, 01:47 PM
Hi,

what you do with this loop is iterating through all rows - assign the value of the DataKey to a string and then assign this variable to your textbox.
Since this is the last value - simply use the last so you don't have to iterate.
Something like TextBox1.Text=gridRows[gridRows.Count-1].GetDataKey...

Regards

Manfred
PS: if this post was helpful please use "Mark as answer"
0
Marcio Nascimento
Top achievements
Rank 1
answered on 26 Aug 2009, 12:20 PM
Hi Manfred,

Thanks for you help. I follow your idea and it works!
But I've been thinking....the procedure (code) to get the KeyValue is diferent depend of the event in question.
For example: If it's the itemInserted event, you have the KeyValue of the directly from the radgrid, but if is the ItemDeleted event you don't have it. So, in this case you have to capture the KeyValue before the item is deleted and store in you database or hidden field.
I think that Telerik should improve this adding a cache for holding the values of a row.

Regards,

Marcio Nascimento
Tags
Grid
Asked by
Marcio Nascimento
Top achievements
Rank 1
Answers by
ManniAT
Top achievements
Rank 2
Marcio Nascimento
Top achievements
Rank 1
Share this question
or