New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Deleting Records

There are generally two ways to perform a delete operation inside RadTreeList:

  • Through the automatic operations provided by the control.

  • Manually, by wiring the ItemCommand or DeleteCommand event of the control.

In both cases, the most straightforward way to trigger the delete is to use a TreeListButtonColumn with a CommandName property set to "Delete".

Deleting records using automatic operations

You can take advantage of the automatic data source operations of RadTreeList by setting up a data source control to allow deletes and binding the treelist to it. Additionally, if you allow cascade deletes for your database, you can set the AllowRecursiveDelete property to "true" which will take care of deleting all child items (if any) of the currently deleted item.

Deleting records manually

The manual delete operations in RadTreeList consist of two main parts:

  1. Accessing the data key value of the item to be deleted.

  2. Using it to find the record inside the treelist's data source and deleting it.

Once the user clicks the delete button inside the treelist, you can handle the DeleteCommand event and perform these steps there. The code snippet below demonstrated a possible approach:

ASPNET
<telerik:RadTreeList RenderMode="Lightweight" ID="RadTreeList1" runat="server" DataKeyNames="ID" ParentDataKeyNames="ParentID"
	AutoGenerateColumns="false" OnNeedDataSource="RadTreeList1_NeedDataSource" OnDeleteCommand="RadTreeList1_DeleteCommand">
	<Columns>
		<telerik:TreeListBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
		</telerik:TreeListBoundColumn>
		<telerik:TreeListBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
		</telerik:TreeListBoundColumn>
		<telerik:TreeListBoundColumn DataField="ParentID" UniqueName="ParentID" HeaderText="ParentID">
		</telerik:TreeListBoundColumn>
		<telerik:TreeListButtonColumn HeaderText="Delete" CommandName="Delete" Text="Delete" />
	</Columns>
</telerik:RadTreeList>

Please note that with this approach you might need to take care of deleting the child items of the deleted item as well.