Ejemplo n.º 1
0
        void grid_AfterCellEdit(object sender, CellAfterEditEventArgs e)
        {
            if (chkOnlyNumeric.Checked)
            {
                if (e.NewData == null || !float.TryParse(e.NewData.ToString(), out var val))
                {
                    if (chkErrorPrompt.Checked)
                    {
                        MessageBox.Show("数字以外の文字は入力できません。");
                    }

                    e.EndReason = EndEditReason.Cancel;
                }
                else
                {
                    e.NewData = val;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Force end current editing operation with the specified reason.
        /// Uses specified data instead of the data of user edited.
        /// </summary>
        /// <param name="data">New data to be set to the edited cell</param>
        /// <param name="reason">Ending Reason of editing operation</param>
        /// <returns>True if currently in editing mode, and operation has been
        /// finished successfully.</returns>
        public bool EndEdit(object data, EndEditReason reason)
        {
            if (currentEditingCell == null || endEditProcessing)
            {
                return(false);
            }

            endEditProcessing = true;

            if (data == null)
            {
                data = this.controlAdapter.GetEditControlText();
            }

            if (AfterCellEdit != null)
            {
                CellAfterEditEventArgs arg = new CellAfterEditEventArgs(currentEditingCell)
                {
                    EndReason = reason,
                    NewData   = data,
                };

                AfterCellEdit(this, arg);
                data   = arg.NewData;
                reason = arg.EndReason;
            }

            switch (reason)
            {
            case EndEditReason.Cancel:
                break;

            case EndEditReason.NormalFinish:
                if (data is string)
                {
                    var datastr = (string)data;

                    if (string.IsNullOrEmpty(datastr))
                    {
                        data = null;
                    }
                    else
                    {
                        double numericValue = 0;

                        // convert data into cell data format
                        switch (currentEditingCell.DataFormat)
                        {
                        case CellDataFormatFlag.Number:
                        case CellDataFormatFlag.Currency:
                            if (double.TryParse(datastr, out numericValue))
                            {
                                data = numericValue;
                            }
                            break;

                        case CellDataFormatFlag.Percent:
                            if (datastr.EndsWith("%"))
                            {
                                double val;
                                if (double.TryParse(datastr.Substring(0, datastr.Length - 1), out val))
                                {
                                    data = val / 100;
                                }
                            }
                            else if (datastr == "%")
                            {
                                data = null;
                            }
                            break;

                        case CellDataFormatFlag.DateTime:
                        {
                            DateTime dt = DateTime.Now;
                            if (DateTime.TryParse(datastr, out dt))
                            {
                                data = dt;
                            }
                        }
                        break;
                        }
                    }
                }

                if (string.IsNullOrEmpty(backupData))
                {
                    backupData = null;
                }

                var body = currentEditingCell.body;

                if (body != null)
                {
                    data = body.OnEndEdit(data);
                }

                if (!object.Equals(data, backupData))
                {
                    DoAction(new SetCellDataAction(currentEditingCell.InternalRow, currentEditingCell.InternalCol, data));
                }

                break;
            }

            this.controlAdapter.HideEditControl();
            this.controlAdapter.Focus();
            currentEditingCell = null;

            endEditProcessing = false;

            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Force end current editing operation with the specified reason.
        /// Uses specified data instead of the data of user edited.
        /// </summary>
        /// <param name="data">New data to be set to the edited cell</param>
        /// <param name="reason">Ending Reason of editing operation</param>
        /// <returns>True if currently in editing mode, and operation has been
        /// finished successfully.</returns>
        public bool EndEdit(object data, EndEditReason reason)
        {
            if (currentEditingCell == null || endEditProcessing)
            {
                return(false);
            }

            endEditProcessing = true;

            if (data == null)
            {
                data = this.controlAdapter.GetEditControlText();
            }

            if (AfterCellEdit != null)
            {
                CellAfterEditEventArgs arg = new CellAfterEditEventArgs(currentEditingCell)
                {
                    EndReason = reason,
                    NewData   = data,
                };

                AfterCellEdit(this, arg);
                data   = arg.NewData;
                reason = arg.EndReason;
            }

            switch (reason)
            {
            case EndEditReason.Cancel:
                break;

            case EndEditReason.NormalFinish:
                if (data is string datastr)
                {
                    data = PostEdit(currentEditingCell, datastr);
                }

                var body = currentEditingCell.body;

                if (body != null)
                {
                    data = body.OnEndEdit(data);
                }

                if (!object.Equals(data, PreEdit(currentEditingCell)))
                {
                    DoAction(new SetCellDataAction(currentEditingCell.InternalPos, data));
                }

                break;
            }

            this.controlAdapter.HideEditControl();
            this.controlAdapter.Focus();
            currentEditingCell = null;

            endEditProcessing = false;

            return(true);
        }