gpt4 book ai didi

c# - 如何创建 datagridview NumericUpDown 列

转载 作者:太空狗 更新时间:2023-10-29 23:39:27 25 4
gpt4 key购买 nike

我想在 datagridview 中添加一个 NumericUpDown 类型的列。所以我为此创建了自定义列类型并且它工作正常但每次都可以看到此控件。我只想在输入此列(NumericUpdown 列)的特定单元格时只显示此控件。我想要如下截图所示。 enter image description here

任何帮助将不胜感激。

最佳答案

我修改了 Web 示例,包括 MIN/MAX 范围。

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);
}
}

关于c# - 如何创建 datagridview NumericUpDown 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18145522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com