deleting from a self-hierarchial grid

4 posts, 0 answers
  1. Raka
    Raka avatar
    71 posts
    Member since:
    Sep 2011

    Posted 17 Jun 2013 Link to this post

    How do I delete rows from a self-hierarchial radgrid?  I want to do this with a 'remove' button click.
    When a row is selected and 'remove' is clicked, I would like to remove the selected row as well as its children.

    Thanks
    Raka
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Jun 2013 Link to this post

    Hi Raka,

    Please try the following code snippet.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" AutoGenerateColumns="False" PageSize="10" AllowSorting="True" AllowPaging="True" GridLines="None" AllowAutomaticDeletes="true">
     <ClientSettings>
        <Selecting AllowRowSelect="true" />
     </ClientSettings>
     <MasterTableView  DataKeyNames="CustomerID" CommandItemDisplay="Top" Name="Customers">
        <DetailTables>
             <telerik:GridTableView DataKeyNames="OrderID" DataSourceID="SqlDataSource2" Width="100%" runat="server" CommandItemDisplay="Top" Name="Orders">
                <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID">
                    </telerik:GridRelationFields>
                 </ParentTableRelation>
        <DetailTables>
          <telerik:GridTableView DataKeyNames="OrderID,ProductID" DataSourceID="SqlDataSource3"  Width="100%" runat="server" CommandItemDisplay="Top" Name="Details">
            <ParentTableRelation>
               <telerik:GridRelationFields DetailKeyField="OrderID" MasterKeyField="OrderID"></telerik:GridRelationFields>
            </ParentTableRelation>
                 <Columns>
                         ... ... ...
                </Columns>
          </telerik:GridTableView>
        </DetailTables>
       <Columns>
          ... ... ...
       </Columns>
      </telerik:GridTableView>
       </DetailTables>
        <Columns>
        ... ... ...
        </Columns>
        <CommandItemTemplate>
          <asp:LinkButton ID="LinkButton1" Height="50px" OnClientClick="javascript:return confirm('Delete all selected customers?')" runat="server" CommandName="DeleteSelected">Delete selected customers</asp:LinkButton>
       </CommandItemTemplate>
     </MasterTableView>
    </telerik:RadGrid>
       <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>"
                DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" SelectCommand="SELECT * FROM [Customers]">
                <DeleteParameters>
                    <asp:Parameter Name="CustomerID" Type="String"></asp:Parameter>
                </DeleteParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>"
                DeleteCommand="DELETE FROM [Orders] WHERE [OrderID] = @OrderID" SelectCommand="SELECT * FROM [Orders]">
             <SelectParameters>
                    <asp:Parameter Name="CustomerID" Type="String"></asp:Parameter>
                </SelectParameters>
                <DeleteParameters>
                    <asp:Parameter Name="OrderID" Type="Int32"></asp:Parameter>
                </DeleteParameters>
      </asp:SqlDataSource>
      <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind_newConnectionString3 %>"
                DeleteCommand="DELETE FROM [Order Details] WHERE [OrderID] = @OrderID AND [ProductID] = @ProductID"
                SelectCommand="SELECT * FROM [Order Details] ">
                <SelectParameters>
                    <asp:Parameter Name="OrderID" Type="Int32"></asp:Parameter>
                </SelectParameters>
                <DeleteParameters>
                    <asp:Parameter Name="OrderID" Type="Int32"></asp:Parameter>
                    <asp:Parameter Name="ProductID" Type="Int32"></asp:Parameter>
             </DeleteParameters>
    </asp:SqlDataSource>

    Thanks,
    Princy
  3. Raka
    Raka avatar
    71 posts
    Member since:
    Sep 2011

    Posted 18 Jun 2013 Link to this post

    Hello Princy

    Thanks for your reply.  However mine is a self referencing grid and so I do not have separate tables.

    It looks like --
    <MasterTableView DataKeyNames="EmployeeId, EmployerId" ...>
            <SelfHierarchySettings ParentKeyName="EmployerId" KeyName="EmployeeId" />
                <Columns>
                    ...
        

    So it needs some sort of cascading or recursive delete.  I am not quite sure how to do it.

    Thanks
    Raka
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 21 Jun 2013 Link to this post

    Hi Raka,

    Please try the following code snippet,its using self referencing hierarchy.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1"
                onitemcommand="RadGrid1_ItemCommand" >
         <ClientSettings Selecting-AllowRowSelect="true">
         </ClientSettings>
             <MasterTableView HierarchyDefaultExpanded="false" HierarchyLoadMode="Client" AllowSorting="true"
                    CommandItemDisplay="Top" DataKeyNames="EmployeeID, ReportsTo" Width="100%" >
                    <SelfHierarchySettings ParentKeyName="ReportsTo" KeyName="EmployeeID" />
                    <CommandItemTemplate>
                        <asp:LinkButton ID="LinkButton1" Height="50px" runat="server" CommandName="DeleteSelected">Delete selected customers</asp:LinkButton>
                    </CommandItemTemplate>
              </MasterTableView>
           <ClientSettings AllowExpandCollapse="true" />
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
      if (e.CommandName == "DeleteSelected")
       {
            //your code to delete
       }
    }

    Thanks,
    Princy
Back to Top