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

Delete a parent item in hierarchy if its last child item is deleted

7 Answers 142 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Donald
Top achievements
Rank 1
Donald asked on 30 Jan 2012, 07:02 AM
Hi,

I have a hierarchical grid with 2 layers (parent level: customer, child level: orders) with a delete column for orders (using Grid_DeleteCommand). When a customer is expanded, and every order of that customer is deleted, instead of the grid showing the expanded customer with "No child records to display.", I'd like that customer deleted.

I can handle this logic in the datasource backend (every time the user deletes an order, I check if there are orders for a left for the customer, if not, delete the customer from the database), but the problem is after Grid_DeleteCommand, Grid_DetailTableDataBind is called and the grid still tries to expand the non-existent customer (ie. the grid still behaves normally, the customer is still shown with the children "No child records to display". The deleted customer only disappears from the grid after a call to Grid_NeedDataSource. How would I get the grid to not call Grid_DetailTableDataBind after the parent has its last child deleted? (and then refresh the parent grid)? Any help would be appreciated.

Thanks,
Donald

7 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Jan 2012, 08:17 AM
Hello Donald,

<MasterTableView DataKeyNames="ID" Name="Parent" HierarchyLoadMode="Client">
              <Columns>
                  <telerik:GridBoundColumn DataField="ID" UniqueName="ID">
                  </telerik:GridBoundColumn>
                  <telerik:GridTemplateColumn>
                      <ItemTemplate>
                          <asp:Button ID="Button1" runat="server" Text="Delete" CommandName="Delete" />
                      </ItemTemplate>
                  </telerik:GridTemplateColumn>
                  <telerik:GridTemplateColumn>
                      <ItemTemplate>
                          <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged"
                              AutoPostBack="true" />
                      </ItemTemplate>
                  </telerik:GridTemplateColumn>
                  <telerik:GridEditCommandColumn UniqueName="MyEditColumn">
                  </telerik:GridEditCommandColumn>
              </Columns>
              <DetailTables>
                  <telerik:GridTableView Name="Child" DataKeyNames="ID">
                      <Columns>
                          <telerik:GridBoundColumn DataField="ID" UniqueName="ID">
                          </telerik:GridBoundColumn>
                          <telerik:GridTemplateColumn>
                              <ItemTemplate>
                                  <asp:Button ID="Button2" runat="server" Text="Delete" CommandName="Delete" />
                              </ItemTemplate>
                          </telerik:GridTemplateColumn>
                          <telerik:GridEditCommandColumn>
                          </telerik:GridEditCommandColumn>
                      </Columns>
                  </telerik:GridTableView>
              </DetailTables>
          </MasterTableView>
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
   {
 
       dynamic data = new[] {
               new { ID = "1", Name ="Name11",ParentID = "0"},
               new { ID = "2", Name ="Name11",ParentID = "1"},
               new { ID = "3", Name ="Name11",ParentID = "2"},
               new { ID = "4", Name ="Name1",ParentID = "3"}
           };
 
       RadGrid1.DataSource = data;
 
   }
 
 
   protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
   {
       GridDataItem item = e.DetailTableView.ParentItem as GridDataItem;
 
       // do your logic here or get Row Count here
       dynamic data = new[] {
               new { ID = "1", Name ="Name11",ParentID = "0"},
                
               new { ID = "4", Name ="Name1",ParentID = "3"}
           };
 
 
       e.DetailTableView.DataSource = data;
 
   }
 
 
protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
   {
       GridDataItem item = e.Item as GridDataItem;
 
       // Do your Delete Logic here
 
       if (item.OwnerTableView.Name == "Child")
       {
           if(item.OwnerTableView.Items.Count == 1)
           {
               // access parent Item
               // and delete this item also
               GridDataItem i1 = item.OwnerTableView.ParentItem as GridDataItem;
                
           }
       }
   }


Thanks,
Jayesh Goyani
0
Donald
Top achievements
Rank 1
answered on 30 Jan 2012, 08:59 AM
Hi Jayesh,

I have just tried your solution, but unfortunately it didn't work for me. To me, it it looks like what you're doing is in the DeleteCommand, if the child item is the last one, delete the parent (I've done that) then you assign the variable
GridDataItem i1 = item.OwnerTableView.ParentItem as GridDataItem;
I have included this new line in my code, but it has not made any difference (as far as I can see). After this, my code still calls RadGrid1_DetailTableDataBind and tries to expand the non-existent parent item (because it was expanded before the DeleteCommand)

Could you explain how this line works?

Thanks,
Donald
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Jan 2012, 09:12 AM
Hello Donald,

griddataitem i1 = item.ownertableview.parentitem as griddataitem;
              i1.expanded = false;

By above code i get the deleted rows parent Item.

Let me know if any concern.

Thanks,
Jayesh Goyani
0
Donald
Top achievements
Rank 1
answered on 30 Jan 2012, 11:32 PM
Hi, I tried adding the extra line, setting the Expanded property of the parent to false, but still no luck. The grid still calls DetailTableDataBind and tries to expand the non-existent parent item.
0
Donald
Top achievements
Rank 1
answered on 31 Jan 2012, 12:07 AM
Hi,

I added some logic to DetailTableDataBind to just return a DataTable with only the columns with no entries, if the grid tries to expand a non-existent table.

Now, when I delete the last child of a parent item, the parent item is collapsed but it's still visible in the grid (even though it is deleted in the codebehind). It takes a refresh of the grid (in my case you must collapse then expand the parent item of the parent item) for the deleted parent to disappear.

How do I get the parent item to not show in the grid?

Your help is appreciated,

Thanks,
Donald
0
Accepted
Daniel
Telerik team
answered on 02 Feb 2012, 02:25 PM
Hi Donald,

I created a simple runnable demo for you. Give it a try and see if it helps.

Thanks,
Daniel
the Telerik team
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 their blog feed now
0
Donald
Top achievements
Rank 1
answered on 07 Feb 2012, 07:01 AM
Hi Daniel,

Your example works great!

Thanks a lot,
Donald
Tags
Grid
Asked by
Donald
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Donald
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or