[原] 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('\n'); 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 } }

 

转载于:https://www.cnblogs.com/Tonyyang/archive/2013/04/02/2994992.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值