Deleting 'Child' Grids in a nested RadGrid

10 posts, 0 answers
  1. Dan
    Dan avatar
    25 posts
    Member since:
    May 2008

    Posted 05 May 2009 Link to this post

     

    In our application we make quite a bit of use of your nested Radgrids, where we must be able to delete each element of the Grid (both parent and children) independently. We have logic that will delete the parent along with all of its children. This is invoked by way of a GridButtonColumn (like the one listed below) which is defined in the parent grid.

     

    Within the parent Grid we have the command:

     

    OnDeleteCommand="WBSGrid_DeleteCommand"

     

    The above method is invoked successfully when we click the GridButtonColumn in the Parent Grid.

     

    Within the GridTableView object contained within the DetailTables portion of the (child) grid we have the following entry:

     

     

    <telerik:GridButtonColumn

        ConfirmText="Delete this Task?"

        ConfirmDialogType="RadWindow"

        ButtonType="ImageButton" 

        ImageUrl="~/images/Delete.gif"

        ConfirmTitle="Delete"

        CommandName="DeleteOption" Text="Delete this Task"

        UniqueName="DeleteOptionColumn"

        HeaderText = "Delete">

        <HeaderStyle Width="35px"  HorizontalAlign="Center"/>

        <ItemStyle HorizontalAlign="Center" />

    </telerik:GridButtonColumn>

     

    When we click one of the Delete Buttons in our child grid, a RadWindow pops up asking us if we wish to delete the entry. So far so good. But if we choose to delete, nothing else happens. Unlike the parent grid, where the OnDeleteCommand element is available to us, there seems to be no corresponding element for the nested grid.

     

    And, if we do find out how to ‘hook up’ our delete button to a code-behind method, we are concerned that it may be awkward to obtain the correct key from the nested grid.

     

    Do you have any suggestions?

  2. Todd Anglin
    Todd Anglin avatar
    2040 posts
    Member since:
    Aug 2005

    Posted 05 May 2009 Link to this post

    Hello Dan-

    I believe that you can trigger the OnDelete event from any button in a RadGrid by making it's CommandName = "Delete". That "reserved" CommandName will automatically be detected and fire the Delete event.

    Once fired, you can easily use the Grid's API to grab the correct key for the item being deleted- even in hierarchal settings. Check out this page in the docs for help with accessing key values:

    http://www.telerik.com/help/aspnet-ajax/grdcascadingdeleteinhierarchicalgrid.html

    It also shows you a way to do "cascading deletes" in RadGrid, which may help show you another approach to your general scenario.

    Hope this helps!
    -Todd
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Dan
    Dan avatar
    25 posts
    Member since:
    May 2008

    Posted 07 May 2009 Link to this post

    Hi:

     

    I’ve done what you suggested and it worked very well. Now we are able to delete either ‘child’ rows or ‘ parent’ rows by clicking on their respective buttons. This is, in large part the behavior we had hoped to achieve and we want to thank you for your help in getting there.

     

    Unfortunately, however there is one more feature that we need to enable which is still not quite working… close – but not quite.

     

    Let’s say that we have a ‘parent’-‘child’ pair in our grid that has just one ‘child’ for that ‘parent’. If the user should click on the delete button associated with the ‘child’ we wish to delete both the ‘child’ and the ‘parent’. By doing this we enforce a vital rule in our system that every ‘parent’ in this grid must have at least one ‘child’.

     

    We have developed code that obtains the appropriate keys (‘child’ and associated ‘parent’), queries the database to determine if this is the only child, and if so, issue a call to the database to delete the parent. All of this codes works and the deletion (of ‘parent’ and ‘child’) does take place. The code follows:

     

    protected void WBSGrid_DeleteCommand(object source, GridCommandEventArgs e)

    {

        WBS_Biz biz = new WBS_Biz(BOEUser);

        //Get the GridEditableItem of the RadGrid    

        GridEditableItem editedItem = (GridEditableItem)e.Item;

        string[] DataKeys = editedItem.OwnerTableView.DataKeyNames;

        // Passing this test indicates that we are trying to delete a 'parent'

        if (DataKeys[0] == "WBSID")

        {

            //Get the primary key value using the DataKeyValue. 

            Int32 wbsID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["WBSID"].ToString());

            try

            {

                biz.DeleteSingleWBS(wbsID);

            }

            catch (Exception ex)

            {

            }

        }

        else

        {

            // falling thru to here means that we are trying to delete a 'child'. When doing so we need to check and see if this

            // 'child' is the only one associated with this 'parent'. If so, we need to delete the 'parent' along with the child.

     

            // get the key of the 'child'...

            Int32 taskID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["TaskID"].ToString());

            // get the 'parent' GridDataItem...

            GridDataItem parentItem = (GridDataItem)(e.Item.OwnerTableView.ParentItem);

            if (parentItem != null)

            {

                // get the key of the 'parent'...

                Int32 wbsID = Convert.ToInt32(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["WBSID"]);

                WBS_TaskData biz2 = new WBS_TaskData();

                DataTable dt = new DataTable();

                try

                {

                    dt = biz2.GetTaskCountByWBS(wbsID);

                    // if the next test returns true, this is the only 'child' for this 'parent' - delete 'parent'...

                    if (dt.Rows.Count == 1)

                    {

                        biz.DeleteSingleWBS(wbsID);

                        WBSGrid.Rebind(); // seems to be needed, grid does not refresh... but this line creates error!!!

                    }

                    else

                    {

                        // otherwise delete 'child'.                      

                        biz.DeleteSingleTask(taskID);

                    }

                }

                catch (Exception ex)

                {

                }

            }

        }

    }

     

    When we (successfully) delete a ‘parent’ in this way (delete ‘parent’… in the code) we discover that the grid does not refresh as it does in all other situations. We tried to force the Rebind by including the WBSGrid.Rebind(); statement. But when we include this line we get the following error message:

     

    There was a problem extracting the DataKeyValues from the DataSource. Please ensure that the

    DataKeyNames are specified correctly and all fields specicied exist within the datasource.

     

    Any suggestions?

  5. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 11 May 2009 Link to this post

    Hello Dan,

    What I would suggest you is to try firing DeleteCommand for the 'parent' row instead of deleting it manually. So instead of the below code:

    if (dt.Rows.Count == 1)  
    {  
        biz.DeleteSingleWBS(wbsID);  
        WBSGrid.Rebind(); // seems to be needed, grid does not refresh... but this line creates error!!!   

    Try using the FireCommand method to force parent item deletion.

    Check it out and let me know if this works for you.

    Best wishes,
    Iana
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Dan
    Dan avatar
    25 posts
    Member since:
    May 2008

    Posted 13 May 2009 Link to this post

    Thanks for your response. I will be trying this today and will report back.

    As always, your assistance is much appreciated.
  7. Dan
    Dan avatar
    25 posts
    Member since:
    May 2008

    Posted 13 May 2009 Link to this post

    I changed the code to read as follows:

     

     

    if (dt.Rows.Count == 1)

    {

        parentItem.FireCommandEvent("Delete", String.Empty);  

    }

     

    The new code does delete the parent and the child. This is desirable. Then it tries to rebind the grid, also desirable. But at some point along the way it raises the following error:

     

    Error: Sys.Webforms.PageRequestManagerServerErrorException: There was a problem extracting DatakeyValues from the DataSource. Please ensure that DataKeyNames are specified correctly and all fields specified exist in the DataSource.

     

    This error seems to come from a JavaScript module called ‘ScriptResource.axd’.

     

    I have tried to step through the code to see if I could isolate the problem further but I had no luck.

     

    So your suggestion worked about as well as what we had been doing before but, unfortunately we still have the same problem.

     

    Any thoughts?

     

  8. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3388 posts

    Posted 14 May 2009 Link to this post

    Hi Dan,

    Could you try canceling the default command running when firing the parentItem deletion:

    if (dt.Rows.Count == 1)  
    {  
        e.Canceled = true;  
        parentItem.FireCommandEvent("Delete", String.Empty);     

    See if ti makes any difference.

    Greetings,
    Iana
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  9. Dan
    Dan avatar
    25 posts
    Member since:
    May 2008

    Posted 14 May 2009 Link to this post

    That was it!!!

    It works beautifully now.

    Thanks so much for your help.
  10. Monalisa
    Monalisa avatar
    35 posts
    Member since:
    May 2009

    Posted 09 Jul 2009 Link to this post

    Hi,

    I have a checkbox present inside the header template of RadGrid and there are no other checkboxes present inside item template. When I check the checkbox all items in RadGrid get selected and then I click on delete button will delete the selected item from the grid. My delete button also not present inside any item template.

    Could anybody help me.
  11. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 09 Jul 2009 Link to this post

    Hi Monalisa,

    Try the following approach to achieve the desired scenario.

    ASPX:
     
    <telerik:GridTemplateColumn UniqueName="SelectCol"  HeaderText="Select"   > 
                    <HeaderTemplate> 
                        <asp:CheckBox ID="CheckBox1" AutoPostBack="true"  runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" /> 
                    </HeaderTemplate> 
                    
                    </telerik:GridTemplateColumn> 

    // To select all the rows on checking the checkbox
    CS:
     
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e) 
        { 
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
            { 
                item.Selected = true
            } 
        } 


    // To delete the selected rows

    CS:
     
     protected void Button1_Click(object sender, EventArgs e) 
        { 
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
            { 
                if (item.Selected) 
                {  
                 //code to delete the selected rows 
                } 
            } 
        } 
     


    Thanks
    Shinu.

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017