Drag and drop from RadListView to RadGridView

1 posts, 0 answers
  1. Вадим
    Вадим avatar
    21 posts
    Member since:
    Mar 2016

    Posted 30 Nov 2017 Link to this post

    <p>private BindingSource _bindingSource;<br>
            DataTable gridViewDataTable = new DataTable();<br>
    <br>
            public SocPersonal()<br>
            {<br>
                          InitializeComponent();<br>            BaseGridBehavior gridBehavior = this.radGridView2.GridBehavior as BaseGridBehavior;<br>            gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));<br>            gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomRowGridBehavior());<br><br>            RadDragDropService dragDropService = radGridView2.GridViewElement.GetService<RadDragDropService>();<br>            dragDropService.PreviewDragStart += svc_PreviewDragStart;<br>            dragDropService.PreviewDragOver += svc_PreviewDragOver;<br>            dragDropService.PreviewDragDrop += svc_PreviewDragDrop;<br>            gridViewDataTable.Columns.Add("key_socRabotnik", typeof(string));<br>            gridViewDataTable.Columns.Add("[ФИО]", typeof(string));<br><br>            radListView1.ListViewElement.DragDropService.PreviewDragOver += PreviewDragOver;<br>            radListView1.ListViewElement.DragDropService.PreviewDragDrop += PreviewDragDrop;<br><br>            MyRussionRadGridLocalizationProvider.CurrentProvider = new MyRussionRadGridLocalizationProvider();<br>            radGridView1.TableElement.Text = MyRussionRadGridLocalizationProvider.TableElementText;<br>            CheckForIllegalCrossThreadCalls = false;    <br>
                HandleCreated += Form_HandleCreated;<br>
            }<br>
    <br>
            #region Drop<br>
            private void svc_PreviewDragStart(object sender, PreviewDragStartEventArgs e)<br>
            {<br>
                e.CanStart = true;<br>
            }<br>
    <br>
            private void svc_PreviewDragOver(object sender, RadDragOverEventArgs e)<br>
            {<br>
                if (e.DragInstance is GridDataRowElement)<br>
                {<br>
                    e.CanDrop = e.HitTarget is DetailListViewDataCellElement ||<br>
                                e.HitTarget is DetailListViewElement;<br>
                }<br>
            }<br>
    <br>
            private void svc_PreviewDragDrop(object sender, RadDropEventArgs e)<br>
            {<br>
                DetailListViewDataCellElement targetCell = e.HitTarget as DetailListViewDataCellElement;<br>
                DetailListViewElement targetElement = e.HitTarget as DetailListViewElement;<br>
                GridDataRowElement draggedRow = e.DragInstance as GridDataRowElement;<br>
    <br>
                if (draggedRow == null)<br>
                {<br>
                    return;<br>
                }<br>
                ListViewDataItem item = new ListViewDataItem();<br>
                DataRow draggedDataBoundItem = ((DataRowView)draggedRow.RowInfo.DataBoundItem).Row;<br>
                if (targetElement != null)<br>
                {<br>
                    ((RadListViewElement)targetElement.Parent).Items.Add(item);<br>
                }<br>
                if (targetCell != null)<br>
                {<br>
                    BaseListViewVisualItem targetVisualItem = targetCell.RowElement;<br>
    <br>
                    int insertIndex = targetCell.Row.ListView.Items.IndexOf(targetVisualItem.Data);<br>
                    if (insertIndex > -1)<br>
                    {<br>
                        targetCell.Row.ListView.Items.Insert(insertIndex, item);<br>
                    }<br>
                }<br>
                item["key_socRabotnik"] = draggedDataBoundItem["key_socRabotnik"];<br>
                item["[ФИО]"] = draggedDataBoundItem["[ФИО]"];<br>
    <br>
                gridViewDataTable.Rows.Remove(draggedDataBoundItem);<br>
            }<br>
    <br>
            private void PreviewDragOver(object sender, RadDragOverEventArgs e)<br>
            {<br>
                e.CanDrop = e.HitTarget is GridTableElement ||<br>
                    e.HitTarget is GridDataRowElement;<br>
            }<br>
    <br>
            private void PreviewDragDrop(object sender, RadDropEventArgs e)<br>
            {<br>
                BaseListViewVisualItem draggedItem = e.DragInstance as BaseListViewVisualItem;<br>
                GridDataRowElement rowElement = e.HitTarget as GridDataRowElement;<br>
                GridTableElement tableElement = e.HitTarget as GridTableElement;<br>
    <br>
                if (rowElement == null && tableElement == null)<br>
                {<br>
                    return;<br>
                }<br>
                e.Handled = true;<br>
                DataRow newRow = gridViewDataTable.NewRow();<br>
                newRow["key_socRabotnik"] = draggedItem.Data["key_socRabotnik"];<br>
                newRow["[ФИО]"] = draggedItem.Data["[ФИО]"];<br>
                if (tableElement != null)<br>
                {<br>
                    gridViewDataTable.Rows.Add(newRow);<br>
                }<br>
                <br>
                if (rowElement != null)<br>
                {<br>
                    GridViewRowInfo targetRow = rowElement.RowInfo;<br>
    <br>
                    int insertIndex = this.radGridView2.Rows.IndexOf(targetRow);<br>
                    if (insertIndex > -1)<br>
                    {<br>
                        gridViewDataTable.Rows.InsertAt(newRow, insertIndex);<br>
                    }<br>
                }<br>
                <br>
    <br>
                this.radListView1.Items.Remove(draggedItem.Data);<br>
            }</p> <p><br></p> <p>private void fillTheDataGrid()<br>
            {<br>
                var commandServer = new CommandServer();<br>
                try<br>
                {<br>
                    radGridView2.Invoke(new MethodInvoker(delegate ()<br>
                    {<br>
                        _bindingSource = new BindingSource { DataSource = commandServer.GetDataGridSet(@"select key_socRabotnik, fio as [ФИО]<br>
                            from socRabotnik<br>
                            where statusDel = 0<br>
                            order by fio").Tables[0] };<br>
                        radGridView2.DataSource = _bindingSource;<br>
    <br>
                        if (radGridView2.Columns.Count > 0)<br>
                        {<br>
                            radGridView2.Columns[0].IsVisible = false;<br>
                            radGridView2.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;<br>
                        }<br>
                        <br>
                        _bindingSource = new BindingSource { DataSource = commandServer.GetDataGridSet(@"select key_socRabotnik, fio as [ФИО]<br>
                            from socRabotnik left join spec_soc on key_socRabotnik = fk_socRabotnik<br>
                            where fk_socRabotnik is null").Tables[0] };<br>
                        radListView1.DataSource = _bindingSource;<br>
                        radListView1.DisplayMember = "[ФИО]";<br>
                        radListView1.ValueMember = "[ФИО]";<br>
                        radListView1.Columns[0].Visible = false;<br>
                    }));<br>
                }<br>
                catch (Exception ex)<br>
                {<br>
                    CommandClient commandClient = new CommandClient();<br>
                    commandClient.WriteFileError(ex, null);<br>
                }<br>
            }</p>


    ListView to GridView: It is dragged, deleted, but not in the grid.
    GridView to ListView: error image
Back to Top