Ejemplo n.º 1
0
        public static void PasteData_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x16)
            {
                e.Handled = true;

                GridView view   = (GridView)sender;
                string   data   = Clipboard.GetText();
                string[] copied = data.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                var rows = view.GetSelectedRows();

                if (rows.Length == 0)
                {
                    //Pasting Column
                    for (int i = 1; i < copied.Length; i++) //Avoid Header
                    {
                        view.AddNewRow();
                        string[] row = copied[i].Split('\t');


                        for (int c = 0; c < row.Length; c++)
                        {
                            if (c >= view.Columns.Count)
                            {
                                break;
                            }

                            GridColumn column = view.GetVisibleColumn(view.FocusedColumn.VisibleIndex + c);

                            if (column.ColumnType.IsSubclassOf(typeof(DBObject)))
                            {
                                RepositoryItemLookUpEdit tmep = column.ColumnEdit as
                                                                RepositoryItemLookUpEdit;

                                if (tmep == null)
                                {
                                    return;
                                }

                                view.SetRowCellValue(view.FocusedRowHandle, column, Session.GetObject(column.ColumnType, tmep.DisplayMember, row[c]));
                            }
                            else if (column.ColumnType == typeof(bool))
                            {
                                view.SetRowCellValue(view.FocusedRowHandle, column, row[c] == "Checked");
                            }
                            else if (column.ColumnType == typeof(decimal) && row[c].EndsWith("%"))
                            {
                                view.SetRowCellValue(view.FocusedRowHandle, column, decimal.Parse(row[c].TrimEnd('%', ' ')) / 100M);
                            }
                            else
                            {
                                view.SetRowCellValue(view.FocusedRowHandle, column, row[c]);
                            }
                        }
                    }
                    return;
                }

                for (int i = 0; i < rows.Length; i++)
                {
                    //Could paste multiple cells;
                    if (i + 1 >= copied.Length)
                    {
                        break;
                    }
                    string[] row = copied[i + 1].Split('\t');

                    var cells = view.GetSelectedCells(rows[i]);

                    if (cells.Length != row.Length)
                    {
                        XtraMessageBox.Show("Column Count does not Copied Column Count");
                        return;
                    }

                    for (int c = 0; c < cells.Length; c++)
                    {
                        GridColumn column = view.Columns[cells[c].FieldName];

                        if (column.ColumnType.IsSubclassOf(typeof(DBObject)))
                        {
                            RepositoryItemLookUpEdit tmep = column.ColumnEdit as RepositoryItemLookUpEdit;

                            if (tmep == null)
                            {
                                return;
                            }

                            view.SetRowCellValue(rows[i], column, Session.GetObject(column.ColumnType, tmep.DisplayMember, row[c]));
                        }
                        else if (column.ColumnType == typeof(bool))
                        {
                            view.SetRowCellValue(rows[i], column, row[c] == "Checked");
                        }
                        else if (column.ColumnType == typeof(decimal) && row[c].EndsWith("%"))
                        {
                            view.SetRowCellValue(rows[i], column, decimal.Parse(row[c].TrimEnd('%', ' ')) / 100M);
                        }
                        else
                        {
                            view.SetRowCellValue(rows[i], column, row[c]);
                        }
                    }
                }
            }
        }