RadGrid KeyValue

7 posts, 1 answers
  1. Marcio Nascimento
    Marcio Nascimento avatar
    54 posts
    Member since:
    Jul 2009

    Posted 22 Aug 2009 Link to this post

    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
  2. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 22 Aug 2009 Link to this post

    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"
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Marcio Nascimento
    Marcio Nascimento avatar
    54 posts
    Member since:
    Jul 2009

    Posted 22 Aug 2009 Link to this post

    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
  5. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 22 Aug 2009 Link to this post

    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"
  6. Marcio Nascimento
    Marcio Nascimento avatar
    54 posts
    Member since:
    Jul 2009

    Posted 22 Aug 2009 Link to this post

    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
  7. Answer
    ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 23 Aug 2009 Link to this post

    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"
  8. Marcio Nascimento
    Marcio Nascimento avatar
    54 posts
    Member since:
    Jul 2009

    Posted 26 Aug 2009 Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017