我想在 datagridview 中添加一个 NumericUpDown 类型的列。所以我为此创建了自定义列类型,它工作正常,但这个控件每次都可见。我只想在输入此列的特定单元格(NumericUpdown 列)时只显示此控件。我想要如下截图所示。
任何帮助将不胜感激。
我修改了包括 MIN/MAX 范围的 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);
}
}
这可能是一种解决方法而不是解决方案,但您可以考虑编写一个简单的自定义表单,当您单击单元格时直接显示在单元格上。该自定义表单将为您提供所需的 numericUpDown 行为,然后只要您在 DataGridView 上的其他任何位置单击返回,自定义表单就会被隐藏,其值将保存到单元格中。似乎这将是一种处理问题并获得相同行为的简单方法。祝你好运。
MSDN 示例代码:DGV_NUPD.exe
选择 3 个 .cs 文件,将它们添加到您的项目中,您就可以开始了
相关 MSDN 教程: http: //msdn.microsoft.com/en-us/library/aa730881 (v=vs.80).aspx