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

[Solved] Extracting data from rows you are deleting

2 Answers 136 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Darryl Chambers
Top achievements
Rank 1
Darryl Chambers asked on 28 May 2009, 07:20 PM
 

How do I extract data (besides the key-id) from a row that I'm deleting? I want to do this from within the method that handles the 'Telerik.Web.UI.RadGrid.DeleteCommandName' event. In that method, I have available a 'Telerik.Web.UI.GridCommandEventArgs' object (e) from which I can extract the ID of the item to be deleted:
        
        
GridEditableItem item = e.Item as GridEditableItem;
        
string recordID = (item.OwnerTableView.DataKeyValues[item.ItemIndex]["RecordID"]).ToString();

 

But I want to get other data, like the "Description" field or "Address" field.
I tried the following (which did not work):

        GridDataItem dataItem = e.Item as GridDataItem;

        string address = (dataItem != null ? dataItem["Address"].Text : "No information about the address.");

I get an exception telling me that it "cannot find a cell bound to the column name 'Address'". But I know the column-and-name is there.

I tried using the unique name of the column (by the way, this is a template column):

        GridDataItem dataItem = e.Item as GridDataItem;

        string address = (dataItem != null ? dataItem["AddressColumn"].Text : "No information about the address.");

But this just returned an empty string. 

How do I proceed?

Thanks in advance.

 

 

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 29 May 2009, 05:50 AM
Hello Darryl,

When using Template Columns you have to access the Control in the ItemTemplate using its ID and then access its text. Check out the sample code to understand better:
aspx:
<telerik:GridTemplateColumn UniqueName="TemplateColumn" DataField="Category">        
        <ItemTemplate> 
         <%#Eval("Name")%> 
          <asp:Label ID="Label" runat="server" Text='<%#Eval("Name")%>'></asp:Label> 
        </ItemTemplate> 
</telerik:GridTemplateColumn> 

c#:
GridDataItem dataItem = (GridDataItem)e.Item; 
string strtxt =((DataBoundLiteralControl)dataItem["TemplateColumn"].Controls[0]).Text; 
string lbltxt = ((Label)dataItem["TemplateColumn"].FindControl("Label")).Text; 

Thanks
Princy.
0
Darryl Chambers
Top achievements
Rank 1
answered on 29 May 2009, 12:24 PM

Eureka!

That worked Princy.

You ARE a Telerik Master.

Thank you very much.

Tags
Grid
Asked by
Darryl Chambers
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Darryl Chambers
Top achievements
Rank 1
Share this question
or