C# 如何创建datagridview NumericUpDown列

C# 如何创建datagridview NumericUpDown列,c#,winforms,C#,Winforms,我想在datagridview中添加一个NumericUpDown类型的列。所以我已经为它创建了自定义列类型,它工作正常,但是这个控件每次都是可见的。我只想当我在该列(NumericUpdown列)的特定单元格中输入时,只显示该控件。我想要如下截图所示。 任何帮助都可以理解。 < P>这可能是解决方案的一种解决方案,但您可以考虑编写一个简单的自定义窗体,当您单击单元格时,该窗体直接显示在单元格上。该自定义表单将为您提供所需的numericUpDown行为,然后只要您在DataGridView上

我想在datagridview中添加一个NumericUpDown类型的列。所以我已经为它创建了自定义列类型,它工作正常,但是这个控件每次都是可见的。我只想当我在该列(NumericUpdown列)的特定单元格中输入时,只显示该控件。我想要如下截图所示。


任何帮助都可以理解。

< P>这可能是解决方案的一种解决方案,但您可以考虑编写一个简单的自定义窗体,当您单击单元格时,该窗体直接显示在单元格上。该自定义表单将为您提供所需的numericUpDown行为,然后只要您在DataGridView上的任何其他位置单击后退,该自定义表单将被隐藏,其值将保存到单元格中。这似乎是处理问题并获得相同行为的简单方法。祝你好运。

MSDN示例代码:

选择3.cs文件,将它们添加到您的项目中即可


相关MSDN教程:

我修改Web示例,包括最小/最大范围

public class NumericUpDownColumn : DataGridViewColumn
{
    public NumericUpDownColumn()
        : base(new NumericUpDownCell())
    {
    }

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

public class NumericUpDownCell : DataGridViewTextBoxCell
{
    private readonly decimal min;
    private readonly decimal max;

    public NumericUpDownCell()
        : base()
    {
        Style.Format = "F0";
    }
    public NumericUpDownCell(decimal min, decimal max)
        : base()
    {
        this.min = min;
        this.max = max;
        Style.Format = "F0";
    }

    public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
        NumericUpDownEditingControl ctl = DataGridView.EditingControl as NumericUpDownEditingControl;
        ctl.Minimum = this.min;
        ctl.Maximum = this.max;
        ctl.Value = Convert.ToDecimal(this.Value);
    }

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

    public override Type ValueType
    {
        get { return typeof(Decimal); }
    }

    public override object DefaultNewRowValue 
    {
        get { return null; } //未編集の新規行に余計な初期値が出ないようにする
    }
}

public class NumericUpDownEditingControl : NumericUpDown, IDataGridViewEditingControl
{
    private DataGridView dataGridViewControl;
    private bool valueIsChanged = false;
    private int rowIndexNum;

    public NumericUpDownEditingControl()
        : base()
    {
        this.DecimalPlaces = 0;
    }

    public DataGridView EditingControlDataGridView
    {
        get { return dataGridViewControl; }
        set { dataGridViewControl = value; }
    }

    public object EditingControlFormattedValue
    {
        get{ return this.Value.ToString("F0"); }
        set{ this.Value = Decimal.Parse(value.ToString()); }
    }
    public int EditingControlRowIndex
    {
        get { return rowIndexNum; }
        set { rowIndexNum = value; }
    }
    public bool EditingControlValueChanged
    {
        get { return valueIsChanged; }
        set { valueIsChanged = value; }
    }

    public Cursor EditingPanelCursor
    {
        get { return base.Cursor; }
    }

    public bool RepositionEditingControlOnValueChange
    {
        get { return false; }
    }

    public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
        this.ForeColor = dataGridViewCellStyle.ForeColor;
        this.BackColor = dataGridViewCellStyle.BackColor;
    }

    public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
    {
        return (keyData == Keys.Left || keyData == Keys.Right ||
            keyData == Keys.Up || keyData == Keys.Down ||
            keyData == Keys.Home || keyData == Keys.End ||
            keyData == Keys.PageDown || keyData == Keys.PageUp);
    }

    public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
    {
        return this.Value.ToString();
    }

    public void PrepareEditingControlForEdit(bool selectAll)
    {
    }

    protected override void OnValueChanged(EventArgs e)
    {
        valueIsChanged = true;
        this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        base.OnValueChanged(e);
    }
}

“如何创建datagridview NumericUpDown列?”非常困难,这就是为什么…David我已经这样做了,但是根据屏幕截图,仍然没有得到desire输出。似乎这个文件不见了。。。谢天谢地,archive.org有一份拷贝。但仍然存在。请注意,使用此功能时,我必须替换
DataGridViewNumericUpDownCell
中的
KeyEnteredItMode
的某些部分,以使其接受azerty上的非键盘数字。最后,这意味着。