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

Drag-Drop item Radtreeview to RadGrid???

1 Answer 84 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
long
Top achievements
Rank 1
long asked on 22 Oct 2010, 05:29 AM
This is my default.aspx
<form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
            <telerik:RadGrid ID="RadGrid1" runat="server"
                GridLines="None">
                <MasterTableView>
                    <CommandItemSettings ExportToPdfText="Export to Pdf" />
                    <RowIndicatorColumn>
                        <HeaderStyle Width="20px" />
                    </RowIndicatorColumn>
                    <ExpandCollapseColumn>
                        <HeaderStyle Width="20px" />
                    </ExpandCollapseColumn>
                </MasterTableView>
            </telerik:RadGrid>
     
        <telerik:RadTreeView ID="RadTreeView1" Runat="server" CheckChildNodes="True"
            CheckBoxes="True" TriStateCheckBoxes="False" EnableDragAndDrop="True"
            EnableDragAndDropBetweenNodes="True" onnodedrop="RadTreeView1_NodeDrop" MultipleSelect="true"  >
            <Nodes>
                <telerik:RadTreeNode runat="server" Text="ThanhPho" Checkable="false"
                    AllowDrag="False">
                    <Nodes>
                        <telerik:RadTreeNode runat="server" Text="HCM" Checkable="False">
                        </telerik:RadTreeNode>
                        <telerik:RadTreeNode runat="server" Text="HaNoi" Checkable="False">
                        </telerik:RadTreeNode>
                        <telerik:RadTreeNode runat="server" Text="DaLat" Checkable="False">
                            <Nodes>
                                <telerik:RadTreeNode runat="server" Text="Ho Xuân Hương" Checkable="true">
                                </telerik:RadTreeNode>
                                <telerik:RadTreeNode runat="server" Text="Thung Lũng Tình Yêu" Checkable="true">
                                </telerik:RadTreeNode>
                            </Nodes>
                        </telerik:RadTreeNode>
                    </Nodes>
                </telerik:RadTreeNode>
                <telerik:RadTreeNode runat="server" Text="Đất Nước" Checkable="false">
                    <Nodes>
                        <telerik:RadTreeNode runat="server" Text="Việt Nam">
                        </telerik:RadTreeNode>
                        <telerik:RadTreeNode runat="server" Text="Singapor">
                        </telerik:RadTreeNode>
                        <telerik:RadTreeNode runat="server" Text="Japan">
                        </telerik:RadTreeNode>
                    </Nodes>
                </telerik:RadTreeNode>
            </Nodes>
        </telerik:RadTreeView>
         
    <asp:Panel ID="Panel1" runat="server" Height="256px">
    <div>
     
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <telerik:RadTextBox ID="RadTextBox1" Runat="server">
            </telerik:RadTextBox>
            <br />
         
         
    </div>
    </asp:Panel>
    <telerik:RadAjaxManager runat="server" ID="RadAjaxManager1">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadTreeView1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadTreeView1" UpdatePanelHeight="" />
                    <telerik:AjaxUpdatedControl ControlID="Panel1" UpdatePanelHeight="" />
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" UpdatePanelHeight="" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    </form>
</body>

And this is my code-behind.
protected void Page_Load(object sender, EventArgs e)
        {
             
                PopulateGrid();
                RadGrid1.Rebind();
            
        }
 
        protected void RadTreeView1_NodeDrop(object sender, Telerik.Web.UI.RadTreeNodeDragDropEventArgs e)
        {
            RadTreeNode sourceNode = e.SourceDragNode;
            RadTreeNode DesNode = e.DestDragNode;
            RadTreeViewDropPosition DropPosition = e.DropPosition;
            if (DesNode != null)
            {
                 
                if (sourceNode.TreeView.SelectedNodes.Count <= 1)
                {
                    PerformDragDrop(DropPosition, sourceNode, DesNode);
                }
                else if (sourceNode.TreeView.SelectedNodes.Count > 1)
                {
                    foreach (RadTreeNode node in sourceNode.TreeView.SelectedNodes)
                    {
                        PerformDragDrop(DropPosition, node, DesNode);
                    }
                }
                DesNode.Expanded = true;
                 
                sourceNode.TreeView.UnselectAllNodes();
            }
            else if (e.HtmlElementID == "TextBox1")
            {
                
                foreach (RadTreeNode node in e.DraggedNodes)
                {
                    TextBox1.Text = node.Text;
                    RadTextBox1.Text = node.Text;
                }
                //DataTable dt = (DataTable)Session["DaTatable"];
                //foreach (RadTreeNode node in e.DraggedNodes)
                //{
                //    string[] value = { node.Text, node.Value };
                //    dt.Rows.Add(value);
                //    RadGrid1.DataSource = dt;
                //    RadGrid1.DataBind();
                //    RadGrid1.Rebind();
                //}
            }
            else if (e.HtmlElementID == RadGrid1.ClientID)
            {
               
                DataTable dt = (DataTable)Session["DaTatable"];
                foreach (RadTreeNode node in e.DraggedNodes)
                {
                    string[] value = { node.Text, node.Value };
                    dt.Rows.Add(value);
                    RadGrid1.DataSource = dt;
                    RadGrid1.DataBind();
                    RadGrid1.Rebind();
                }
            }
 
        }
        private static void PerformDragDrop(RadTreeViewDropPosition DropPosition, RadTreeNode sourceNode, RadTreeNode desNode)
        {
            switch (DropPosition)
            {
                case RadTreeViewDropPosition.Over:
                    // child
                    if (!sourceNode.IsAncestorOf(desNode))
                    {
                        sourceNode.Owner.Nodes.Remove(sourceNode);
                        desNode.Nodes.Add(sourceNode);
                    }
                    break;
                case RadTreeViewDropPosition.Above:
                    // sibling - above
                    sourceNode.Owner.Nodes.Remove(sourceNode);
                    desNode.InsertBefore(sourceNode);
                    break;
                case RadTreeViewDropPosition.Below:
                    // sibling - below
                    sourceNode.Owner.Nodes.Remove(sourceNode);
                    desNode.InsertAfter(sourceNode);
                    break;
            }
        }
        private void PopulateGrid()
        {
            string[] values = { "One", "Two" };
 
            DataTable dt = new DataTable();
            dt.Columns.Add("Item");
            dt.Columns.Add("Price");
            //dt.Columns.Add("Category");
            //dt.Rows[0]["Item"]=values[0];
            //dt.Rows[0]["Price"] = values[1];
            dt.Rows.Add(values);
            dt.Rows.Add(values);
            Session["DataTable"] = dt;
 
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
            RadGrid1.Rebind();
        }
When I dragged and dropped item treeview to textbox >>> It was fine. But when I dragged and dropped item treeview to radgrid >>> It didn't work . I had tested when I dragged and dropped into radgrid with a message alert >>> seem my "(e.HtmlElementID == RadGrid1.ClientID)" doesn't work because no message appeared. >>>> What 's wrong ???? Thanks a lot !!!

1 Answer, 1 is accepted

Sort by
0
Nikolay Tsenkov
Telerik team
answered on 27 Oct 2010, 10:13 AM
Hello long,

Have you looked at this demo: http://demos.telerik.com/aspnet-ajax/treeview/examples/functionality/draganddropnodes/defaultcs.aspx

Please, try define what is the difference between the two approaches and let me know if you still are having a hard time work it out.

Hope this is helpful for you!


Regards,
Nikolay Tsenkov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
TreeView
Asked by
long
Top achievements
Rank 1
Answers by
Nikolay Tsenkov
Telerik team
Share this question
or