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

[Solved] How to get the datakey of a newly row

5 Answers 367 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric Ulliac
Top achievements
Rank 1
Eric Ulliac asked on 17 Jun 2008, 12:27 PM
Hi,

I need to get the dataKey of the newly added row after automatic insert in a RadGrid.
It's possible to get it with the ItemInserted event function?
I tried to get the dataKey like as
protected void MyRadGrid_ItemInserted(object o, GridInsertedEventArgs e) 
   if (e.Exception != null) 
   { 
      e.ExceptionHandled = true
      //Error messsage here 
   } 
   else 
   { 
      GridEditableItem An_item = (GridEditableItem)e.Item; 
      int The_data_key = int.Parse(An_item.GetDataKeyValue("Id").ToString()); 
   } 
But this code works only in the ItemUpdated case.

5 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 17 Jun 2008, 01:34 PM
Hello Eric,

You can use the ItemCommand event handler, after checking for PerformInsert command name.

Sincerely yours,
Yavor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Eric Ulliac
Top achievements
Rank 1
answered on 17 Jun 2008, 03:16 PM
I follow your instructions but I don't manage to get the new dataKey Value
protected void MyRadGrid_ItemCommand(object o, GridCommandEventArgs e) 
        if (e.CommandName == "PerformInsert") 
        { 
            GridEditableItem Un_item = (GridEditableItem)e.Item; 
            int The_key_value = int.Parse(Un_item.GetDataKeyValue("Id").ToString()); 
        } 
    } 
 

It retrun a ArgumentOutOfRangeException at the execution.

0
Yavor
Telerik team
answered on 18 Jun 2008, 07:36 AM
Hello Eric,

You can actually access the insert/update form controls (Un_item["ColumnName"].controls[0] as TextBox).
If this is an insert operation, you can cast the item to GridEditFormInsertItem (as opposed to GridEditableItem).
I hope this information helps.

Sincerely yours,
Yavor
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Eric Ulliac
Top achievements
Rank 1
answered on 18 Jun 2008, 08:01 AM
Hi Yavor,

So sorry beacause I miss to tell you one detail :
The Id field is an auto increment field in my table, that why I can't retrive it on the insert form.
I need to get this auto created id after an auto insert in my radGrid.
0
Yavor
Telerik team
answered on 18 Jun 2008, 11:23 AM
Hello Eric,

Unfortunately, when this is the case, it is beyond the control of the grid product, and into the realm of the underlying database. You may consider using triggers, in a fashin similar to this.

Greetings,
Yavor
the Telerik team

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