[원본] XAF ListView 행 또는 셀 붙여넣기
// , , Excel ListView 。
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Win.Editors;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Xpo;
using DevExpress.Data.Filtering;
namespace TestWinEditorCsharp.Module.Controllers
{
    public partial class PasteViewController : ViewController
    { 
        public PasteViewController()
        {
            InitializeComponent();
            RegisterActions(components);
            TargetViewType = ViewType.ListView;
        }
            protected override void OnViewControlsCreated()
            {
                base.OnViewControlsCreated();
                // copy should not include column headers
                GridListEditor listEditor = ((ListView)View).Editor as GridListEditor;
                if (listEditor != null)
                {
                    listEditor.GridView.OptionsBehavior.CopyToClipboardWithColumnHeaders = false;
                }
            }
            #region Paste Rows
            private void PasteRowAction_Execute(object sender, SimpleActionExecuteEventArgs e)
            {
                string[][] copiedValues = ClipboardDataToJaggedArray();
                if (copiedValues == null) return;
                PasteRowValues(copiedValues);
            }
            private void PasteRowValues(string[][] copiedValues)
            {
                GridListEditor listEditor = ((ListView)View).Editor as GridListEditor;
                if (listEditor != null)
                {
                    XafGridView gridView = listEditor.GridView;
                    Session session = ((XPObjectSpace)ObjectSpace).Session;
                    // paste rows
                    if ((gridView.IsNewItemRow(gridView.FocusedRowHandle)))
                    {
                        // note that the new row must be focused for this to work
                        PasteValuesToNewRows(copiedValues, gridView, session, gridView.FocusedRowHandle);
                    }
                    else
                    {
                        // paste to selected rows
                        int[] selectedRowHandles = gridView.GetSelectedRows();
                        PasteValuesToExistingRows(copiedValues, gridView, session, selectedRowHandles);
                    }
                }
            }
            private void PasteValuesToExistingRows(string[][] copiedValues, XafGridView gridView, Session session, int[] selectedRowHandles)
            {
                string keyName = GetKeyName();
                int focusedRowHandle = gridView.FocusedRowHandle;
                // paste rows
                for (int r = 0; r < copiedValues.Length; r++)
                {
                    // ignore row with empty string
                    if (copiedValues[r].Length == 1 && string.IsNullOrWhiteSpace(copiedValues[r][0]))
                        continue;
                    // get next selected row
                    if (r < selectedRowHandles.Length)
                        focusedRowHandle = selectedRowHandles[r];
                    else
                        return;
                    // paste cells
                    PasteColumnsToRow(copiedValues[r], gridView, session, focusedRowHandle, keyName);
                    gridView.UpdateCurrentRow();
                }
            }
            // newRowHandle: must be new row handle.
            private void PasteValuesToNewRows(string[][] copiedValues, XafGridView gridView, Session session, int newRowHandle)
            {
                string keyName = GetKeyName();
                // paste rows
                for (int r = 0; r < copiedValues.Length; r++)
                {
                    // ignore row with empty string
                    if (copiedValues[r].Length == 1 && string.IsNullOrWhiteSpace(copiedValues[r][0]))
                        continue;
                    // add new row in gridview
                    gridView.FocusedRowHandle = newRowHandle;
                    gridView.AddNewRow();
                    // paste cells
                    PasteColumnsToRow(copiedValues[r], gridView, session, newRowHandle, keyName);
                    gridView.UpdateCurrentRow();
                }
            }
            private void PasteColumnsToRow(string[] copiedRowValues, XafGridView gridView, Session session, int focusedRowHandle, string keyName)
            {
                for (int c = 0; c < gridView.Columns.Count && c < copiedRowValues.Length; c++)
                {
                    var gridViewColumn = (XafGridColumn)gridView.Columns[c];
                    // skip non-editable column
                    bool allowEdit = gridViewColumn.Model.AllowEdit;
                    if (!allowEdit)
                        continue;
                    // skip Key
                    if (gridViewColumn.PropertyName == keyName)
                        continue;
                    string lookupProperty = gridViewColumn.Model.LookupProperty;
                    object pasteValue;
                    // set object to paste based on lookup property
                    if (string.IsNullOrEmpty(lookupProperty))
                        pasteValue = copiedRowValues[c];
                    else
                        pasteValue = session.FindObject(gridView.Columns[c].ColumnType,
                            CriteriaOperator.Parse(string.Format("{0} LIKE '{1}'", lookupProperty, copiedRowValues[c])));
                    // paste object to new cell
                    gridView.SetRowCellValue(focusedRowHandle, gridView.Columns[c], pasteValue);
                }
            }
            #endregion
            #region Paste Cells
            // ensure that you set the below:
            // gridView1.OptionsBehavior.CopyToClipboardWithColumnHeaders = false;
            private void PasteCellValueAction_Execute(object sender, SimpleActionExecuteEventArgs e)
            {
                string[] copiedValues = ClipboardDataColumnToArray(0);
                if (copiedValues == null) return;
                PasteCellValues(copiedValues);
            }
            // paste single value into selected rows in the focused column
            // if the property editor is a lookup, then a lookup is performed on the paste value
            private void PasteCellValues(string copiedValue)
            {
                string[] copiedValues = new string[1];
                copiedValues[0] = copiedValue;
                PasteCellValues(copiedValues);
            }
            private void PasteCellValues(string[] copiedValues)
            {
                GridListEditor listEditor = ((ListView)View).Editor as GridListEditor;
                if (listEditor != null)
                {
                    XafGridView gridView = listEditor.GridView;
                    bool allowEdit = ((XafGridColumn)gridView.FocusedColumn).Model.AllowEdit;
                    if (!allowEdit)
                        throw new InvalidOperationException("Column '" + gridView.FocusedColumn.Caption + "' is not editable.");
                    Session session = ((XPObjectSpace)ObjectSpace).Session;
                    string lookupProperty = ((XafGridColumn)gridView.FocusedColumn).Model.LookupProperty;
                    int[] selectedRows = gridView.GetSelectedRows();
                    int copyIndex = 0;
                    foreach (int rowHandle in selectedRows)
                    {
                        // set object to paste based on lookup property
                        string copiedValue = copiedValues[copyIndex];
                        object pasteValue;
                        if (string.IsNullOrEmpty(lookupProperty))
                            pasteValue = copiedValue;
                        else
                            pasteValue = session.FindObject(gridView.FocusedColumn.ColumnType,
                                CriteriaOperator.Parse(string.Format("{0} LIKE '{1}'", lookupProperty, copiedValue)));
                        // paste object to focused cell
                        gridView.SetRowCellValue(rowHandle, gridView.FocusedColumn, pasteValue);
                        // go to next copied value
                        copyIndex++;
                        // reset copied row counter to the beginning after the last row is reached
                        if (copyIndex > copiedValues.Length - 1)
                            copyIndex = 0;
                    }
                }
            }
            #endregion
            #region XAF
            public string GetKeyName()
            {
                return View.Model.AsObjectView.ModelClass.KeyProperty;
            }
            #endregion
            #region Clipboard
            // split tab-delimited string into array
            string[] GetRowData(string data)
            {
                string[] rowData = data.Split(new char[] { '\r', '\x09' });
                return rowData;
            }
            string ClipboardData
            {
                get
                {
                    System.Windows.Forms.IDataObject iData = System.Windows.Forms.Clipboard.GetDataObject();
                    if (iData == null) return "";
                    if (iData.GetDataPresent(System.Windows.Forms.DataFormats.Text))
                        return (string)iData.GetData(System.Windows.Forms.DataFormats.Text);
                    return "";
                }
                set { System.Windows.Forms.Clipboard.SetDataObject(value); }
            }
            private string[] ClipboardDataColumnToArray(int column = 0)
            {
                string[][] data = ClipboardDataToJaggedArray();
                if (data == null) return null;
                string[] result = new string[data.Length];
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = data[i][column];
                }
                return result;
            }
            private string[][] ClipboardDataToJaggedArray()
            {
                string[] data = ClipboardData.Split('
');
                if (data.Length < 1) return null;
                string[][] parsed = new string[data.Length][];
                for (int i = 0; i < parsed.Length; i++)
                {
                    parsed[i] = GetRowData(data[i]);
                }
                return parsed;
            }
            #endregion
        }
    }이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.