阅读背景:

DEV 的Gridiew Ctrl + C 和 Ctrl + V

来源:互联网 
 #region Copy & Paste - 
        private bool ctrlCDown = false;
        private bool ctrlVDown = false;
        /// <summary>
        /// 
        /// </summary>
        /// <![CDATA[
        /// For Copy Function your GridView should set like below
        /// gridView.OptionsView.AllowCellMerge = false;
        /// gridView.OptionsSelection.MultiSelect = true;
        /// gridView.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CellSelect;
        /// ]]>
        /// <param name="gridViews"></param>
        public void SetCopyAndPasteFunction(GridColumn[] usedColumns, params GridView[] gridViews)
        {
            try
            {
                foreach (GridView gridView in gridViews)
                {
                    #region KeyDown Event Part
                    gridView.KeyDown += new KeyEventHandler(
                        delegate(object sender, KeyEventArgs e)
                        {
                            GridView currentGridView = sender as GridView;
                            if (currentGridView == null) return;
                            System.Diagnostics.Debug.WriteLine(e.KeyCode);
                            if (e.Control)
                            {
                                //gvList.CloseEditor();
                                System.Diagnostics.Debug.WriteLine("Ctrl+" + e.KeyCode);
                                if (e.KeyValue == ((int)'C') || e.KeyValue == ((int)'c')) //Copy
                                {
                                    ctrlCDown = true;
                                }
                                else if (e.KeyValue == ((int)'V') || e.KeyValue == ((int)'v')) //Paste
                                {
                                    
                                    // 修改问题124:数字不能 Ctrl+C/V  -> 请修改成可以。 (Main信息 – Pattern基准Spec也要)
                                    // islocked=false页面锁定状态不能粘贴进来
                                    if (currentGridView.Editable == true && currentGridView.FocusedColumn != null && currentGridView.FocusedRowHandle >= 0 && currentGridView.FocusedRowHandle < currentGridView.DataRowCount)
                                    {
                                        if (ctrlCDown == true)
                                        {
                                            ctrlCDown = false;
                                            RemoveClipBoardHeader();
                                        }

                                        string text = null;
                                        IDataObject data = Clipboard.GetDataObject();
                                        if (data.GetDataPresent(DataFormats.Text))
                                        {
                                            text = data.GetData(DataFormats.Text).ToString();
                                            string[] splits = text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                                            List<GridColumn> columnList = new List<GridColumn>();
                                           
                                            //bool isFirst = false;
                                            string caption = splits[0];
                                            string[] captionSplit = caption.Split('\t');

                                            int focusedColumnIndex = currentGridView.FocusedColumn.VisibleIndex;
                                           
                                            if (currentGridView.GetSelectedCells().Length > 1)
                                            {
                                                focusedColumnIndex = currentGridView.FocusedColumn.VisibleIndex;
                                            }
                                            foreach (GridColumn col in usedColumns)
                                            {
                                                if (col.VisibleIndex >= focusedColumnIndex && col.Visible == true)
                                                {
                                                    columnList.Add(col);
                                                }
                                            }
                                            columnList.Sort(new Comparison<GridColumn>(
                                                delegate(GridColumn source, GridColumn target)
                                                {
                                                    return source.VisibleIndex - target.VisibleIndex;
                                                }
                                            ));


                                            string[] cellSplits = null;
                                            int rowHandle = currentGridView.FocusedRowHandle;
                                            int availableCellLength = columnList.Count;
                                            currentGridView.UpdateCurrentRow();
                                            currentGridView.CloseEditor();
                                            currentGridView.BeginDataUpdate();
                                            currentGridView.BeginSelection();
                                            ctrlVDown = true;
                                            try
                                            {
                                                List<string> splitsList = new List<string>();
                                                if (splits.Length == 1)
                                                {
                                                    captionSplit[0] = gvList.FocusedColumn.Caption;
                                                    splitsList.Add(gvList.FocusedColumn.Caption);
                                                    splitsList.Add(splits[0]);
                                                }
                                                else
                                                {
                                                    splitsList = splits.ToList();
                                                }
                                                for (int i = 1; i < splitsList.Count && rowHandle < currentGridView.DataRowCount; i++)
                                                {
                                                    List<string> list = new List<string>();
                                                    for (int z = 0; z < usedColumns.Length; z++)
                                                    {
                                                        //if(usedColumns[z].FieldName!=cbbStdSize.SelectedItem.ToString())
                                                        list.Add(usedColumns[z].Caption.Replace("\r\n", "").Replace("\n", ""));
                                                    }

                                                    cellSplits = splitsList[i].Split('\t');
                                                    if (availableCellLength > cellSplits.Length) availableCellLength = cellSplits.Length;

                                                    for (int j = 0; j < availableCellLength; j++)//availableCellLength
                                                    {
                                                        if (columnList[j].OptionsColumn.AllowEdit == false || columnList[j].OptionsColumn.ReadOnly == true)
                                                        {
                                                            //availableCellLength = j;
                                                            list.Remove(columnList[j].FieldName);
                                                            continue;
                                                        }
                                                        else if (j < columnList.Count)
                                                        {
                                                            try
                                                            {
                                                                for (int z = 0; z < captionSplit.Length; z++)
                                                                {
                                                                    if (list.Contains(captionSplit[z]))
                                                                    {
                                                                        if (cellSplits[z] == "")
                                                                            currentGridView.SetRowCellValue(rowHandle, columnList[j], 0);
                                                                        else
                                                                            currentGridView.SetRowCellValue(rowHandle, columnList[j], cellSplits[z]);//cellSplits[k]
                                                                        list.Remove(captionSplit[z]);
                                                                        break;
                                                                    }
                                                                }
                                                            }
                                                            catch
                                                            {
                                                                if (i == 0 && j == 0) return;
                                                                availableCellLength = j;
                                                                break;
                                                            }
                                                            currentGridView.SelectCell(rowHandle, columnList[j]);
                                                        }
                                                        else break;
                                                    }
                                                    rowHandle++;
                                                }
                                            }
                                            finally
                                            {
                                                currentGridView.EndSelection();
                                                currentGridView.EndDataUpdate();
                                                e.Handled = true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    );
                    #endregion

                    #region KeyUp Event Part
                    gridView.KeyUp += new KeyEventHandler(delegate(object sender, KeyEventArgs e)
                    {
                        if (e.Control)
                        {
                            if (e.KeyValue == ((int)'C') || e.KeyValue == ((int)'c')) //Copy
                            {
                                if (ctrlCDown == true)
                                {
                                    ctrlCDown = false;
                                    RemoveClipBoardHeader();
                                }
                            }
                        }
                    }
                    );
                    #endregion

                    #region CustomRowCellEditForEditing Event Part

                    gridView.CustomRowCellEditForEditing += new CustomRowCellEditEventHandler(delegate(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
                    {
                        if (ctrlVDown == true)
                        {
                            if (e.RepositoryItem != null) e.RepositoryItem.ReadOnly = true;
                            ctrlVDown = false;
                        }
                        else
                        {
                            if (e.RepositoryItem != null) e.RepositoryItem.ReadOnly = false;
                        }
                    }
                    );
                    #endregion

                    gridView.InvalidValueException += new DevExpress.XtraEditors.Controls.InvalidValueExceptionEventHandler(
                        delegate(object sender, DevExpress.XtraEditors.Controls.InvalidValueExceptionEventArgs e)
                        {
                            if (ctrlVDown == true) e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
                        });
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        private void RemoveClipBoardHeader()
        {
            string text = null;
            IDataObject data = Clipboard.GetDataObject();
            if (data.GetDataPresent(DataFormats.Text))
            {
                text = data.GetData(DataFormats.Text).ToString();
                string[] splits = text.Split(new string[] { "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < splits.Length; i++)
                {
                    if (i > 0)
                        break;
                    string newstr = splits[0].Replace("\n", string.Empty);
                    splits[0] = newstr;
                }
                string value = string.Empty;
                if (splits != null && splits.Length > 1)
                {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < splits.Length; i++)
                    {
                        sb.AppendLine(splits[i]);
                    }
                    value = sb.ToString();
                }
                SetClipBoardText(value);
            }
        }

        private void SetClipBoardText(string text)
        {
            IDataObject data = Clipboard.GetDataObject();
            data.SetData(text);
            Clipboard.SetDataObject(data);
            Clipboard.SetText(text);
        }

        #endregion #region Copy & Paste - 
        private bool c



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: