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.
The manual delete operations in RadTreeList consist of two main parts:
Accessing the data key value of the item to be deleted.
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:
protectedvoidRadTreeList1_NeedDataSource(object sender,TreeListNeedDataSourceEventArgs e){
RadTreeList1.DataSource =GetDataTable();}protectedvoidRadTreeList1_DeleteCommand(object sender,TreeListCommandEventArgs e){//accessing the datakey value through the server API of the TreeListDataItemstring dataKeyValue =(e.Item asTreeListDataItem).GetDataKeyValue("ID").ToString();//the actual deletion logic depends on the kind of data source that you use, your only task is to remove the item from the data source. The RadTreeList control will implicitly rebind itself afterwards.String ConnString = ConfigurationManager.ConnectionStrings["TelerikConnectionString"].ConnectionString;SqlConnection conn =newSqlConnection(ConnString);SqlCommand deleteCommand =newSqlCommand("DELETE FROM SelfReferencing WHERE ID='"+ dataKeyValue +"'", conn);
conn.Open();try{
deleteCommand.ExecuteNonQuery();}finally{
conn.Close();}}publicDataTableGetDataTable(){String ConnString = ConfigurationManager.ConnectionStrings["TelerikConnectionString"].ConnectionString;SqlConnection conn =newSqlConnection(ConnString);SqlDataAdapter adapter =newSqlDataAdapter();
adapter.SelectCommand =newSqlCommand("SELECT ID, Name, ParentID FROM SelfReferencing", conn);DataTable myDataTable =newDataTable();
conn.Open();try{
adapter.Fill(myDataTable);}finally{
conn.Close();}return myDataTable;}
VB.NET
ProtectedSub RadTreeList1_NeedDataSource(ByVal sender AsObject,ByVal e As TreeListNeedDataSourceEventArgs)
RadTreeList1.DataSource = GetDataTable()EndSubProtectedSub RadTreeList1_DeleteCommand(ByVal sender AsObject,ByVal e As TreeListCommandEventArgs)'accessing the data key value through the server API of the TreeListDataItemDim dataKeyValue AsString=TryCast(e.Item, TreeListDataItem).GetDataKeyValue("ID").ToString()'the actual deletion logic depends on the kind of data source that you use, your only task is to remove the item from the data source. The RadTreeList control will implicitly rebind itself afterwards.Dim ConnString As [String] = ConfigurationManager.ConnectionStrings("TelerikConnectionString").ConnectionString
Dim conn AsNew SqlConnection(ConnString)Dim deleteCommand AsNew SqlCommand("DELETE FROM SelfReferencing WHERE ID='"+ dataKeyValue +"'", conn)
conn.Open()Try
deleteCommand.ExecuteNonQuery()Finally
conn.Close()EndTryEndSubPublicFunction GetDataTable()As DataTable
Dim ConnString As [String] = ConfigurationManager.ConnectionStrings("TelerikConnectionString").ConnectionString
Dim conn AsNew SqlConnection(ConnString)Dim adapter AsNew SqlDataAdapter()
adapter.SelectCommand =New SqlCommand("SELECT ID, Name, ParentID FROM SelfReferencing", conn)Dim myDataTable AsNew DataTable()
conn.Open()Try
adapter.Fill(myDataTable)Finally
conn.Close()EndTryReturn myDataTable
EndFunction
Please note that with this approach you might need to take care of deleting the child items of the deleted item as well.