具有不同值和显示方式的自定义DataGridViewColumn

时间:2019-04-18 02:19:34

标签: c# winforms datagridview

我正在研究自定义DataGridViewColumn,允许用户输入类似1:30的值。

如果用户将其保存到Database,则我有一个函数将其存储为十进制值,因此1:30的for将变为1.5。数据库列是float

现在,我的问题是输入1:30之类的时间格式时,单元格不接受。细胞失去焦点后,值消失。那么是否可以在类型为double的单元格中显示字符串格式(1:30)?怎么样?你能告诉我一些代码吗?谢谢。

自定义DataGridViewColumn的代码:

public class OptTimeColumn : DataGridViewColumn
{
   public OptTimeColumn() : base(new OptTimeCell())
   {

   }

  public override DataGridViewCell CellTemplate
  {
     get
        {
            return base.CellTemplate;
        }
     set
        {
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(OptTimeCell)))
            {
                throw new InvalidCastException("Must be a OptTimeCell");
            }
            base.CellTemplate = value;
        }
    }
}

public class OptTimeCell : DataGridViewTextBoxCell
{
    public OptTimeCell() : base() 
    {
    }

    public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
        OptTimeColumnEditingControl ctl = DataGridView.EditingControl as OptTimeColumnEditingControl;

        OptTimeColumn col = (OptTimeColumn)this.OwningColumn;

        if (this.Value == null)
        {
            ctl.DecimalValue = Convert.ToDouble(this.DefaultNewRowValue);
        }
        else
        {
            ctl.DecimalValue = Convert.ToDouble(this.Value);
        }
    }

    public override Type EditType
    {
        get
        {
            return typeof(OptTimeColumnEditingControl);
        }
    }

    public override Type ValueType
    {
        get
        {
            //return typeof(string);
            return typeof(double);
        }
    }

    public override object DefaultNewRowValue
    {
        get
        {
            //return string.Empty;
            return DBNull.Value;
        }
    }
}
public class OptTimeColumnEditingControl : OptTime.OptTime, IDataGridViewEditingControl
{
    DataGridView dataGridView;
    private bool valueChanged = false;
    int rowIndex;

    public OptTimeColumnEditingControl()
    {

    }
    public object EditingControlFormattedValue
    {
        get
        {
            return this.DecimalValue;
        }
        set
        {
            if (value is double)
            {
                try
                {
                    this.DecimalValue = Convert.ToDouble(value);
                }
                catch
                {
                    this.DecimalValue = 0;
                }
            }
        }
    }

    public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
    {
        return EditingControlFormattedValue;
    }
    public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
    }
    public int EditingControlRowIndex
    {
        get
        {
            return rowIndex;
        }
        set
        {
            rowIndex = value;
        }
    }
    public bool EditingControlWantsInputKey(Keys key, bool dataGridViewWantsInputKey)
    {
        switch (key & Keys.KeyCode)
        {
            case Keys.Left:
            case Keys.Up:
            case Keys.Down:
            case Keys.Right:
            case Keys.Home:
            case Keys.End:
            case Keys.PageDown:
            case Keys.PageUp:
                return true;
            default:
                return !dataGridViewWantsInputKey;
        }
    }
    public void PrepareEditingControlForEdit(bool selectAll)
    {
    }
    public bool RepositionEditingControlOnValueChange
    {
        get
        {
            return false;
        }
    }
    public DataGridView EditingControlDataGridView
    {
        get
        {
            return dataGridView;
        }
        set
        {
            dataGridView = value;
        }
    }
    public bool EditingControlValueChanged
    {
        get
        {
            return valueChanged;
        }
        set
        {
            valueChanged = value;
        }
    }
    public Cursor EditingPanelCursor
    {
        get
        {
            return base.Cursor;
        }
    }

    protected override void OnTextChanged(EventArgs eventargs)
    {
        valueChanged = true;
        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        base.OnTextChanged(eventargs);
    }
}

1 个答案:

答案 0 :(得分:0)

  1. 很抱歉这样问,但是:Why-oh-why are you saving time to db as double?您的数据库不支持Time或DateTime数据类型吗?
  2. 您使用哪种方法来与db-ORM(例如EF)交互?还有吗将UI直接依赖于存储或存储在UI上通常非常糟糕。在两者之间使用一些层。理想地探索MVC,MVVM和DI / IoC。
  3. 要实现所需的功能(例如在EF中),可以利用内置的ViewModel模式,您可以在序列化之前或反序列化之后将数据转换为自己喜欢的格式。视图(网格)应设置为您希望用户看到的数据类型,例如“时间”,并且您的存储类型应设置为您希望存储的类型,例如“ VARCHAR(32)”-那么您在ViewModel中实现的逻辑将需要实现两者之间的转换(希望是无损的)。