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

RadGrid Self-referencing Hierarchy - Editing problems

1 Answer 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tomaž Lovrec
Top achievements
Rank 1
Tomaž Lovrec asked on 21 May 2010, 11:06 AM
Hi,

I'e followed this demo to get a treeview-like radgrid:
http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/selfreferencing/defaultcs.aspx

But the problems start at editing already inserted items.
Only the root(top-parent) item can be edited. If I click on the edit pencil icon on an item that has a parrent, that item dissapears from the gird, but if I later click on a root item both items get opened for editing.
Editing mode is on PopUp with user control.

And here is the page:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="pm_tasks.aspx.cs" Inherits="members_pm_tasks" %> 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>  
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"
    <script type="text/javascript"
        var combo; 
        function nodeClicking(sender, args) { 
            var combocomboBox = combo; 
 
            var node = args.get_node() 
 
            comboBox.set_text(node.get_text()); 
            comboBox.value = node.get_value(); 
 
            comboBox.trackChanges(); 
            comboBox.commitChanges(); 
 
            comboBox.hideDropDown(); 
        } 
 
        function StopPropagation(e) { 
            if (!e) { 
                e = window.event; 
            } 
 
            e.cancelBubble = true
        } 
 
        function OnClientDropDownOpenedHandler(sender, args) { 
            combo = sender
            var tree = sender.get_items().getItem(0).findControl("rtv_parent"); 
            var selectedNode = tree.get_selectedNode(); 
            if (selectedNode) { 
                selectedNode.scrollIntoView(); 
            } 
        }    
    </script> 
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"
        <AjaxSettings> 
            <telerik:AjaxSetting AjaxControlID="RadGrid1"
                <UpdatedControls> 
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
                </UpdatedControls> 
            </telerik:AjaxSetting> 
            <telerik:AjaxSetting AjaxControlID="chk_end"
                <UpdatedControls> 
                    <telerik:AjaxUpdatedControl ControlID="chk_end" /> 
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
                </UpdatedControls> 
            </telerik:AjaxSetting> 
            <telerik:AjaxSetting AjaxControlID="ddl_project"
                <UpdatedControls> 
                    <telerik:AjaxUpdatedControl ControlID="ddl_project" /> 
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" /> 
                </UpdatedControls> 
            </telerik:AjaxSetting> 
        </AjaxSettings> 
    </telerik:RadAjaxManager> 
     
    <table> 
        <tr> 
            <td> 
                IZBERI PROJEKT:&nbsp; 
            </td> 
            <td> 
                <asp:DropDownList ID="ddl_project" runat="server" DataTextField="title" DataValueField="id" DataSourceID="SqlProject" 
                    OnSelectedIndexChanged="project_SelectedIndexChanged" OnDataBound="project_DataBound" AutoPostBack="true" Filter="Contains" AllowCustomText="true"
                </asp:DropDownList> 
                <asp:SqlDataSource ID="SqlProject" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
                    SelectCommand="SELECT [id], [title] FROM pm_projects"
                </asp:SqlDataSource> 
            </td> 
            <td> 
                &nbsp;<asp:CheckBox ID="chk_end" runat="server" OnCheckedChanged="chk_endChanged" Checked="true" AutoPostBack="true" /> 
            </td> 
            <td> 
                PRIKAŽI SAMO NEDOKONČANE NALOGE 
            </td> 
        </tr> 
    </table> 
    <br /><br /> 
     
    <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlTask" OnColumnCreated="RadGrid1_ColumnCreated" 
        OnItemCreated="RadGrid1_ItemCreated" OnInsertCommand="taskInsertCommand"  
        OnItemDataBound="RadGrid1_ItemDataBound" GridLines="None" Skin="Default>" 
        <MasterTableView HierarchyDefaultExpanded="true" HierarchyLoadMode="Client" AllowSorting="true" 
            DataKeyNames="id" Width="100%" AutoGenerateColumns="False"  
            DataSourceID="SqlTask" GridLines="None" AllowPaging="True" EditMode="PopUp"
            <SelfHierarchySettings ParentKeyName="id_parent" KeyName="id" /> 
            <Columns> 
                <telerik:GridBoundColumn DataField="id" DataType="System.Int32" HeaderText="ID"  
                    ReadOnly="True" SortExpression="id" UniqueName="id" HeaderStyle-Width="150px"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="title" DataType="System.String"  
                    HeaderText="NASLOV" SortExpression="title"  
                    UniqueName="title" HeaderStyle-Width="150px"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="start_date" DataType="System.DateTime" 
                    HeaderText="DATUM ZAČETKA" SortExpression="start_date" UniqueName="start_date" HeaderStyle-Width="150px"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="task_creator" DataType="System.Guid"  
                    HeaderText="USTVARJALEC" SortExpression="task_creator"  
                    UniqueName="task_creator" HeaderStyle-Width="150px"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="task_assigned" DataType="System.Guid"  
                    HeaderText="IZVAJALEC" SortExpression="task_assigned"  
                    UniqueName="task_assigned" HeaderStyle-Width="150px"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="deadline" DataType="System.DateTime" 
                    HeaderText="ROK" SortExpression="deadline" UniqueName="deadline" HeaderStyle-Width="150px"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="percent_complete" DataType="System.Int32"  
                    HeaderText="KONČANO V %" SortExpression="percent_complete"  
                    UniqueName="percent_complete" HeaderStyle-Width="150px"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="end_date" DataType="System.DateTime" 
                    HeaderText="DATUM KONCA" SortExpression="end_date" UniqueName="end_date" HeaderStyle-Width="150px"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="predecessors" HeaderText="PREDHODNIKI"  
                    SortExpression="predecessors" UniqueName="predecessors" HeaderStyle-Width="150px"
                </telerik:GridBoundColumn> 
                <telerik:GridBoundColumn DataField="id_parent" DataType="System.Int32"  
                    HeaderText="id_parent" SortExpression="id_parent" UniqueName="id_parent" Visible="true" HeaderStyle-Width="150px"
                </telerik:GridBoundColumn> 
                <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Preview" Text="Predogled" ImageUrl="~/members/images/magnifier.png" UniqueName="Preview"
                   <HeaderStyle Width="25px" />                  
               </telerik:GridButtonColumn>             
                <telerik:GridEditCommandColumn ButtonType="ImageButton" CancelText="Preklici"  
                    InsertText="Dodaj" UpdateText="Shrani"
                    <HeaderStyle Width="32px" /> 
                </telerik:GridEditCommandColumn> 
               <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete"  
                   ConfirmDialogType="RadWindow" ConfirmText="Ali si preprican da želiš izbrisati vnos ?" 
                   Text="Briši" UniqueName="column"
                   <HeaderStyle Width="32px" /> 
               </telerik:GridButtonColumn>                 
               <telerik:GridTemplateColumn AllowFiltering="false"
                   <ItemTemplate> 
                       <img src="images/calendar.gif" style="cursor:pointer;" alt="" onclick="return Wopen('<%# Eval("id") %>');" /> 
                   </ItemTemplate> 
                   <HeaderStyle Width="32px" /> 
               </telerik:GridTemplateColumn>   
            </Columns> 
            <EditFormSettings CaptionFormatString="UREJANJE NALOG" UserControlName="user_controls/insertTask.ascx"  
                EditFormType="WebUserControl" PopUpSettings-Width="600"
                <EditColumn ButtonType="ImageButton" UniqueName="EditCommandColumn1"
                </EditColumn> 
                <PopUpSettings ScrollBars="None"></PopUpSettings> 
            </EditFormSettings> 
        </MasterTableView> 
        <ClientSettings AllowExpandCollapse="true" /> 
    </telerik:RadGrid> 
    <asp:SqlDataSource ID="SqlTask" runat="server"  
        ConnectionString="<%$ ConnectionStrings:myConnectionString %>" 
        OnSelecting="SqlTask_Selecting" 
        SelectCommand="SELECT [id], [title], [start_date], [end_date], (select ime + ' ' + priimek from Calendar where guid_calendar = t.task_creator) as [task_creator], 
            (select ime + ' ' + priimek from Calendar where guid_calendar = t.task_assigned) as [task_assigned], [deadline], 
            [percent_complete], [predecessors], [id_parent] 
            FROM [pm_tasks] t 
            WHERE t.id_project = @id_project AND t.percent_complete &lt; @percent_max"> 
            <SelectParameters> 
                <asp:Parameter Name="id_project" DefaultValue="1" /> 
                <asp:Parameter Name="percent_max" DefaultValue="100" /> 
            </SelectParameters> 
    </asp:SqlDataSource> 
</asp:Content> 
 
 


And the code behind:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
using System.Reflection; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.Web.Services; 
using System.Web.Security; 
using System.Collections; 
using System.Diagnostics; 
using System.Text; 
using System.IO; 
using System.Web.Profile; 
using System.Xml.Linq; 
 
public partial class members_pm_tasks : System.Web.UI.Page 
    private int ddl_index; 
    private DataClassesDataContext db = new DataClassesDataContext(); 
 
    protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e) 
    { 
        if (e.Column is GridExpandColumn) 
        { 
            e.Column.Visible = false
        } 
        else if (e.Column is GridBoundColumn) 
        { 
            e.Column.HeaderStyle.Width = Unit.Pixel(150); 
        } 
    } 
 
    public void Page_Load(object sender, EventArgs e) 
    { 
        if (Assembly.GetAssembly(typeof(ScriptManager)).FullName.IndexOf("3.5") != -1 || Assembly.GetAssembly(typeof(ScriptManager)).FullName.IndexOf("4.0") != 1) 
        { 
            RadGrid1.MasterTableView.FilterExpression = @"it[""id_parent""] = Convert.DBNull"
        } 
        else 
        { 
            RadGrid1.MasterTableView.FilterExpression = "id_parent IS NULL"
        } 
 
        if (!IsPostBack) 
        { 
            try 
            { 
                ddl_index = System.Convert.ToInt32(Request.QueryString["ddl_index"]); 
                if (ddl_index != 0) 
                { 
                    ddl_project.SelectedValue = ddl_index.ToString(); 
                } 
            } 
            catch { ddl_index = 0; } 
        } 
    } 
 
    public void Page_PreRenderComplete(object sender, EventArgs e) 
    { 
        HideExpandColumnRecursive(RadGrid1.MasterTableView); 
    } 
 
    public void HideExpandColumnRecursive(GridTableView tableView) 
    { 
        GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView); 
        foreach (GridNestedViewItem nestedViewItem in nestedViewItems) 
        { 
            foreach (GridTableView nestedView in nestedViewItem.NestedTableViews) 
            { 
                nestedView.Style["border"] = "0"
 
                Button MyExpandCollapseButton = (Button)nestedView.ParentItem.FindControl("MyExpandCollapseButton"); 
                if (nestedView.Items.Count == 0) 
                { 
                    if (MyExpandCollapseButton != null
                    { 
                        MyExpandCollapseButton.Style["visibility"] = "hidden"
                    } 
                    nestedViewItem.Visible = false
                } 
                else 
                { 
                    if (MyExpandCollapseButton != null
                    { 
                        MyExpandCollapseButton.Style.Remove("visibility"); 
                    } 
                } 
 
                if (nestedView.HasDetailTables) 
                { 
                    HideExpandColumnRecursive(nestedView); 
                } 
            } 
        } 
    } 
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        CreateExpandCollapseButton(e.Item, "id"); 
 
        if (e.Item is GridHeaderItem && e.Item.OwnerTableView != RadGrid1.MasterTableView) 
        { 
            e.Item.Style["display"] = "none"
        } 
 
        if (e.Item is GridNestedViewItem) 
        { 
            e.Item.Cells[0].Visible = false
        } 
    } 
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        CreateExpandCollapseButton(e.Item, "id"); 
        RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top; 
        RadGrid1.MasterTableView.CommandItemSettings.AddNewRecordText = "NOVO OPRAVILO"
        RadGrid1.MasterTableView.CommandItemSettings.RefreshText = "OSVEŽI"
    } 
 
    public void CreateExpandCollapseButton(GridItem item, string columnUniqueName) 
    { 
        if (item is GridDataItem) 
        { 
            if (item.FindControl("MyExpandCollapseButton") == null
            { 
                Button button = new Button(); 
                button.Click += new EventHandler(button_Click); 
                button.CommandName = "ExpandCollapse"
                button.CssClass = (item.Expanded) ? "rgCollapse" : "rgExpand"
                button.ID = "MyExpandCollapseButton"
 
                if (item.OwnerTableView.HierarchyLoadMode == GridChildLoadMode.Client) 
                { 
                    string script = String.Format(@"$find(""{0}"")._toggleExpand(this, event); return false;", item.Parent.Parent.ClientID); 
 
                    button.OnClientClick = script; 
                } 
 
                int level = item.ItemIndexHierarchical.Split(':').Length; 
                if (level > 1) 
                { 
                    button.Style["margin-left"] = level * 10 + "px"
                } 
 
                TableCell cell = ((GridDataItem)item)[columnUniqueName]; 
                cell.Controls.Add(button); 
                cell.Controls.Add(new LiteralControl("&nbsp;")); 
                cell.Controls.Add(new LiteralControl(((GridDataItem)item).GetDataKeyValue(columnUniqueName).ToString())); 
            } 
        } 
    } 
 
    void button_Click(object sender, EventArgs e) 
    { 
        ((Button)sender).CssClass = (((Button)sender).CssClass == "rgExpand") ? "rgCollapse" : "rgExpand"
    } 
 
    protected void SqlTask_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
    { 
        if (ddl_project.SelectedValue != ""
            e.Command.Parameters["@id_project"].Value = ddl_project.SelectedValue; 
        if (chk_end.Checked) 
            e.Command.Parameters["@percent_max"].Value = "100"
        else 
            e.Command.Parameters["@percent_max"].Value = "101"
    } 
 
    protected void project_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        RadGrid1.Rebind(); 
    } 
 
    protected void project_DataBound(object sender, EventArgs e) 
    { 
        RadGrid1.Rebind(); 
    } 
 
    protected void chk_endChanged(object sender, EventArgs e) 
    { 
        RadGrid1.Rebind(); 
    } 
 
    protected void taskInsertCommand(object source, GridCommandEventArgs e) 
    { 
        UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID); 
 
        pm_task task = new pm_task(); 
        updateTask(ref task, userControl); 
        db.pm_tasks.InsertOnSubmit(task); 
        db.SubmitChanges(); 
    } 
 
    private void updateTask(ref pm_task task, UserControl userControl) 
    { 
        task.id_project = System.Convert.ToInt32((userControl.FindControl("ddl_project"as DropDownList).SelectedValue); 
 
        if (!(userControl.FindControl("chk_noParent"as CheckBox).Checked) 
        { 
            RadComboBox rcb_parent = userControl.FindControl("rcb_parent"as RadComboBox; 
            RadTreeView rtv_parent; 
            foreach (RadComboBoxItem item in rcb_parent.Items) 
            { 
                rtv_parent = item.FindControl("rtv_parent"as RadTreeView; 
                task.id_parent = System.Convert.ToInt32(rtv_parent.SelectedValue); 
            } 
        } 
        else 
            task.id_parent = null
 
        task.task_creator = new Guid("8A936ACE-9A27-4008-84B8-367378518F49");//(Guid)Membership.GetUser().ProviderUserKey; 
        task.task_assigned = new Guid((userControl.FindControl("ddl_assigned"as DropDownList).SelectedValue); 
        task.title = (userControl.FindControl("txt_title"as TextBox).Text; 
        task.start_date = (DateTime)(userControl.FindControl("dtp_start"as RadDateTimePicker).SelectedDate; 
        task.deadline = (DateTime)(userControl.FindControl("dtp_deadline"as RadDateTimePicker).SelectedDate; 
        task.description = (userControl.FindControl("txt_description"as TextBox).Text; 
        task.predecessors = getPredecessors(userControl.FindControl("rcb_predecessors"as RadComboBox); 
    } 
 
    private string getPredecessors(RadComboBox rcb) 
    { 
        string predecessors = ""
        CheckBox cb; 
        foreach (RadComboBoxItem item in rcb.Items) 
        { 
            cb = item.FindControl("check"as CheckBox; 
            if (cb.Checked) 
                predecessors += item.Value + ";"
        } 
        return predecessors; 
    } 
 
 
 


Any help with this?

Best regards,
Thomas





1 Answer, 1 is accepted

Sort by
0
BaiH
Top achievements
Rank 1
answered on 26 May 2010, 12:12 PM
You should use NeedDataSource instead of populating the grid with DataSourceControl, if you want to use editing with self-referencing hierarchy.
It is stated in the docs http://www.telerik.com/help/aspnet-ajax/grdselfreferencinghierarchy.html (near the bottom of the page).

--BH
Tags
Grid
Asked by
Tomaž Lovrec
Top achievements
Rank 1
Answers by
BaiH
Top achievements
Rank 1
Share this question
or