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

RadGrid DeleteCommand

4 Answers 190 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Roman
Top achievements
Rank 1
Roman asked on 06 Nov 2009, 10:54 AM
Hello my dear RadGrid friends!

In my project I implemented a RadGrid with a custom data access, nothing like all the examples provided (direct sql, etc.). Here's my NeedDataSource method:

        /// <summary> 
        /// Defining the PartnerListGrid's data, i.e. providing a datascource 
        /// </summary> 
        /// <param name="source"></param> 
        /// <param name="e"></param> 
        protected void PartnerListGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
        { 
            PartnerAgent partneragent = new PartnerAgent(); 
            Partner[] partners = partneragent.Load(); 
 
            if (!displayMessages(partneragent)) 
            { 
                var partnerselects = from p in partners 
                                     select new 
                                     { 
                                         ID = p.PartnerID, 
                                         Name = p.Name, 
                                         Type = p.Partnertype.Name, 
                                         Contacts = String.Join("<br />", p.Contacts.Select<Contact, String>(cn => String.Concat(cn.Prename, " ", cn.Name)).ToArray()), 
                                         Mainaddress = p.Mainaddress, 
                                         Links = new HyperLink[] { new HyperLink() { Text = "Projekte" }, new HyperLink() { Text = "Passwörter" }, new HyperLink() { Text = "Keys" }, new HyperLink() { Text = "PCs" }, new HyperLink() { Text = "Server" }, new HyperLink() { Text = "Domains" } } 
                                     }; 
 
                PartnerListGrid.DataSource = partnerselects; 
            } 
        } 

Now I want to get the provided row by in the DeleteCommand, but all I could find was a text-attribute from a cell containing something half-way similar like the DataItem.
Unfortunately, the DeleteCommand's DataItem is empty.
Looks like following:
        protected void PartnerListGrid_DeleteCommand(object source, GridCommandEventArgs e) 
        { 
            GridDataItem x = ((RadGrid)source).Items[e.Item.ItemIndex]; 
            var HereIsSomeDataAvailable = x.Cells[2].Text; 
             
            // HOW??? 
 
            /*var activerow = InformationAgent.CastByExample(x.Cells[2].Text, new { ID = Guid.Empty, Name = "", Type = "", Contacts = "", Mainaddress = "", Links = new HyperLink[] { new HyperLink() { Text = "test" }, new HyperLink() { Text = "test2" } } });
            PartnerAgent partneragent = new PartnerAgent();
            partneragent.deletePartner(activerow.ID);
            displayMessages(partneragent);*/ 
        } 

The part on the bottom, commented out, is my previous try, however, unfortunately the data from the cell is not convertable to the previously generated anonymous type.

Please tell me if you need any further data to provide help.
And also don't hesitate to tell me if I've chosen a wrong/chaotic way to work with the RadGrid ^^

Thanks in advance for your help!
Best
Roman



4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 06 Nov 2009, 11:51 AM
Hi Roman,

Generally DataItem is available only during data-binding. On post-back events (when the grid is not rebound) the grid (and the grid child controls) will be recreated from ViewState and that is why DataItem is null. You should use DataKeyNames/DataKeyValues across post-backs to check desired key value!

Best wishes,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Roman
Top achievements
Rank 1
answered on 06 Nov 2009, 12:23 PM
Ah I see, I will :) Thanks for the tip!
Best
Roman
0
Nicolaï
Top achievements
Rank 2
answered on 15 Oct 2013, 07:21 AM
What if it's a radtreelist, and you need some dataitem in the deleteCommand?
Can't add a key...
(Only way I found is to have an extra column with Display=false and read the .text of the cell... Feels dirty..?)
0
Kostadin
Telerik team
answered on 18 Oct 2013, 06:02 AM
Hi Nicolaï,

The best solution is to add the required fields into the DataKeyNames collection and then retrieve their values in code behind by using the DataKeyValues. I would recommend you to review the following live example which demonstrates this approach. If you cannot use this approach then you have to get the text from some of the other column. You could do that  by using their ColumnUnique names as described in the following help topic.

Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Roman
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Roman
Top achievements
Rank 1
Nicolaï
Top achievements
Rank 2
Kostadin
Telerik team
Share this question
or