仅允许datagridview中的列仅插入数字

时间:2017-08-02 03:50:12

标签: c#

我有一个函数只将我的datagridview(Line ID)的第一列限制为一个数字。但是,当我单击第一列时,它不仅限制我的第一列,而且限制整个datagridview只输入数字。以下是我的代码。

class App extends Component {
    state = {
       name: 'John'
    }

    render() {
        return  <div>{this.state.name}</div>
    }

}

1 个答案:

答案 0 :(得分:0)

我这样做的方法是将NumericUpDown控件分配给datagrid视图。但您必须将此作为自定义列。如果您愿意,可以将相同的概念用于其他类型的控制。

你需要

  • 数字列
  • 数字细胞控制
  • 数字向上单元格

在您的datagridview中分配了NumericColumn。 该列包含NumericCellControl 其中包含NumericUpDownCell进行编辑

以下是我使用的代码,其中一些是根据我的要求自定义的,因此您必须相应地更新。例如,我有固定的最小值,最大值和默认值1.0。

<强> NumericUpDownCell

class NumericUpDownCell: DataGridViewTextBoxCell
{
    public NumericUpDownCell(): base()
    {
        this.Style.Format = "0.0";
    }

    public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        // Set the value of the editing control to the current cell value.
        base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
        NumericCellControl nUpDown = DataGridView.EditingControl as NumericCellControl;

        // Use the default row value when Value property is null.
        if (this.Value == null)
        {
            nUpDown.Value = (Decimal)this.DefaultNewRowValue;
        }
        else
        {
            //nUpDown.Value = Decimal.Parse(this.Value.ToString());
            Object trueValue = this.Value;
            nUpDown.Value = Decimal.Parse(trueValue.ToString());
        }
    }

    public override Type EditType
    {
        get
        {
            // Return the type of the editing control 
            return typeof(NumericCellControl);  
        }
    }

    public override Type ValueType
    {
        get
        {
            return base.ValueType;
        }
        set
        {
            base.ValueType = value;
        }
    }

    public override object DefaultNewRowValue
    {
        get
        {
            // Use 1.0 as the default value.
            return 1.0m;
        }
    }
}   

<强> NumericCellControl

public class NumericCellControl : NumericUpDown, IDataGridViewEditingControl
{

    private bool Cancelling = false;

    public NumericCellControl()
    {
        this.Increment = 0.1m;
        this.DecimalPlaces = 1;
        this.Minimum = 1;
        this.Maximum = 20;
    }

    // Implements the IDataGridViewEditingControl.EditingControlFormattedValue property.
    public Object EditingControlFormattedValue
    {
        get
        {
            // must return a String
            // it doesn't matter if the value is formatted, it will be replaced
            // by the formatting events
            return this.Value.ToString();
        }

        set
        {
            decimal val = 0;
            if (value is decimal)
                val = (decimal)value;
            else
            {
                String s = "" + value;
                if (s.Length > 0)
                {
                    decimal.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, out val);
                }
            }

            if (val >= this.Minimum && val <= this.Maximum)
                this.Value = val;
        }
    }

    protected override void OnLeave(EventArgs e)
    {
        if (!Cancelling)
        {
            var dgv = this.EditingControlDataGridView;
            var cell = (NumericUpDownCell)dgv.CurrentCell;
            cell.Value = this.Value;
        }

        base.OnLeave(e);
        Cancelling = false;
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
        {
            Cancelling = true;
            e.Handled = true;
            e.SuppressKeyPress = true;
            var dgv = this.EditingControlDataGridView;
            dgv.CancelEdit();
            dgv.EndEdit();
        }

        base.OnKeyDown(e);
    }

    // Implements the  IDataGridViewEditingControl.GetEditingControlFormattedValue method. 
    public Object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
    {
        return EditingControlFormattedValue;
    }

    // Implements the IDataGridViewEditingControl.ApplyCellStyleToEditingControl method. 
    public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
    {
        this.Font = dataGridViewCellStyle.Font;
        this.ForeColor = dataGridViewCellStyle.ForeColor;
        this.BackColor = dataGridViewCellStyle.BackColor;
    }

    // Implements the IDataGridViewEditingControl.EditingControlRowIndex property. 
    public int EditingControlRowIndex { get; set; }

    // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey method. 
    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:
            case Keys.Escape:                
                return true;
            default:
                return !dataGridViewWantsInputKey;
        }
    }

    // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit method. 
    public void PrepareEditingControlForEdit(bool selectAll)
    {
        // No preparation needs to be done.
    }

    // Implements the IDataGridViewEditingControl.RepositionEditingControlOnValueChange property. 
    public bool RepositionEditingControlOnValueChange
    {
        get
        {
            return false;
        }
    }

    // Implements the IDataGridViewEditingControl.EditingControlDataGridView property. 
    public DataGridView EditingControlDataGridView { get; set; }

    // Implements the IDataGridViewEditingControl.EditingControlValueChanged property. 
    public bool EditingControlValueChanged { get; set; }

    // Implements the IDataGridViewEditingControl.EditingPanelCursor property. 
    public Cursor EditingPanelCursor
    {
        get
        {
            return base.Cursor;
        }
    }
}

<强> NumericColumn

public class NumericColumn : DataGridViewColumn
{
    public NumericColumn() : base(new NumericUpDownCell())
    {
        this.ValueType = typeof(decimal?);
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            // Ensure that the cell used for the template is correct.
            if (value != null && !value.GetType().IsAssignableFrom(typeof(NumericUpDownCell)))
            {
                throw new InvalidCastException("Must be a NumericUpDownCell");
            }
            base.CellTemplate = value;
        }
    }
}

<强>用法

然后您可以按如下方式使用

//Create new Numeric Column
NumericColumn nCol = new NumericColumn();
nCol.ReadOnly = false;
nCol.Name = "MyColumnName";
nCol.HeaderText = "My Numeric Column";
nCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
nCol.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

//Assign it to your datagridview
myDatagridview.Columns.Add(nCol);