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

[Solved] Get Item Name From Grid

4 Answers 343 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Darren
Top achievements
Rank 2
Darren asked on 03 Jun 2009, 08:36 AM
Hi there,

Currently I'm giving notifications like so:
        public static string ItemUpdated(object source, GridUpdatedEventArgs e, RadGrid RadGrid1) 
        { 
            GridEditableItem item = (GridEditableItem)e.Item; 
            string displayMsg = string.Empty; 
            string name = item.OwnerTableView.Name; 
            foreach(string dataKeyName in RadGrid1.MasterTableView.DataKeyNames) 
            { 
                string id = item.GetDataKeyValue(dataKeyName).ToString(); 
 
                if (e.Exception != null
                { 
                    e.KeepInEditMode = true
                    e.ExceptionHandled = true
                    displayMsg = string.Format("{0} with ID " + id + " cannot be updated. Reason: " + e.Exception.Message, name); 
                } 
                else 
                { 
                    displayMsg = string.Format("{0} with ID " + id + " is updated!", name);                     
                } 
            } 
            return displayMsg; 
        } 

It's probably simple but instead of getting the Id of the record I'm working with I'd like to get one of the column values. Lets say I have a column of Name. Then I'd like to get the name value and produce the message "'Name' was updated" instead of what I have now which gives me the Id.

Thanks :)


4 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 03 Jun 2009, 10:42 AM
Hello Darren,

If you are using the InPlace edit mode then you wouldnt be able to get the parent item of the edited item but you can set the Name field as the DataKeyName and then access the value as shown below:
aspx:
  <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" OnItemUpdated="RadGrid1_ItemUpdated" runat="server">       
       <MasterTableView DataKeyNames="Id,Name" > 

c#:
protected void RadGrid1_ItemUpdated(object source, GridUpdatedEventArgs e)  
        {  
            GridEditableItem item = (GridEditableItem)e.Item;                         
            string strname = item.GetDataKeyValue("Name").ToString();  
  
        }  

Whereas if you are using the EditForms edit mode, Then you can access the parent item as shown below:
c#:
protected void RadGrid1_ItemUpdated(object source, GridUpdatedEventArgs e)  
        {  
            GridEditableItem item = (GridEditableItem)e.Item;  
            GridDataItem parentItem = (GridDataItem)item.ParentItem;  
            string strname = parentItem["Name"].Text;  
         } 
  
          

-Princy.
0
Darren
Top achievements
Rank 2
answered on 03 Jun 2009, 11:25 AM
Hi Princy,

I copied this code because I'm using EditForms:
protected void RadGrid1_ItemUpdated(object source, GridUpdatedEventArgs e)  
        {  
            GridEditableItem item = (GridEditableItem)e.Item;  
            GridDataItem parentItem = (GridDataItem)item.ParentItem;  
            string strname = parentItem["FirstName"].Text;  
         } 
But I received an error on ParentItem
" 'Telerik.Web.UI.GridEditableItem' does not contain a definition for 'ParentItem' and no extension method 'ParentItem' accepting a first argument of type 'Telerik.Web.UI.GridEditableItem' could be found (are you missing a using directive or an assembly reference?)"

I changed ParentItem to Parent and my project compiled but then I get this error at runtime.
"Unable to cast object of type 'Telerik.Web.UI.GridTable' to type 'Telerik.Web.UI.GridDataItem'.


0
Accepted
Princy
Top achievements
Rank 2
answered on 03 Jun 2009, 11:37 AM
Hello Darren,

I'm sorry, you would have to try accessing the edit item as EditFormItem. Here is the correct code:
c#:
protected void RadGrid1_ItemUpdated(object source, GridUpdatedEventArgs e)   
        {   
            GridEditFormItem item = (GridEditFormItem)e.Item;   
            GridDataItem parentItem = (GridDataItem)item.ParentItem;   
            string strname = parentItem["FirstName"].Text;   
         }  
Sorry for any inconvenience this might have caused.

Regards
Princy.
0
Darren
Top achievements
Rank 2
answered on 03 Jun 2009, 11:44 AM
Thank you Princy for you reply.
The code works and you've definitely not inconvenienced me, quite the oopposite.
Your always so helpful.

Thanks again!
Tags
Grid
Asked by
Darren
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Darren
Top achievements
Rank 2
Share this question
or