求助一个NumericUpDown的问题
我想在一个datagridview里面为每一条记录嵌入不同的编辑器,例如NumericUpDown,Combobox等等,差不多完成了。
但是遇到一个问题,我发现在NumericUpDown直接输入数值时只要输入0就会触发ValueChanged 事件,例如输入20就相当于2+enter。我试验了一下,如果NumericUpDown不在DataGridView里面,输入0不会触发ValueChanged 事件。
这到底是咋回事呀?
附上代码
但是遇到一个问题,我发现在NumericUpDown直接输入数值时只要输入0就会触发ValueChanged 事件,例如输入20就相当于2+enter。我试验了一下,如果NumericUpDown不在DataGridView里面,输入0不会触发ValueChanged 事件。
这到底是咋回事呀?
附上代码
//初始化表格
private void init()
{
SplashScreen.BeginDisplay();
myDgvNoAdjInPara.AutoGenerateColumns = false;
myDgvNoAdjInPara.Controls.Add(paraNumericUpDown);
myDgvNoAdjInPara.Controls.Add(paraComboBox);
myDgvNoAdjInPara.Controls.Add(paraRadioButton);
paraControl.Visible = false;
paraNumericUpDown.Visible = false;
paraComboBox.Visible = false;
paraRadioButton.Visible = false;
paraNumericUpDown.ValueChanged += frmTestItem_ValueChanged;
paraComboBox.SelectedIndexChanged += frmTestItem_SelectedIndexChanged;
paraRadioButton.CheckedChanged += frmTestItem_CheckedChanged;
}
//点击一个单元格,显示不同的编辑器
private void myDgvNoAdjInPara_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (e.RowIndex < 0) return;
if (dgv.Rows[e.RowIndex].Cells["colId"].Value == null) return;
paraControl.Visible = false;
int id = Convert.ToInt32(dgv.Rows[e.RowIndex].Cells["colId"].Value);
Rectangle rect;
if (dgv.Columns[e.ColumnIndex].HeaderText == "参数值")
{
var itemPara = TestItemEquipIOParaService.SelectById(id);
if (itemPara == null || itemPara.ParaCode == null) return;
var equipPara = EquipParaService.SelectByCode(itemPara.ParaCode);
if (equipPara == null || equipPara.ParaValueType == null) return;
getParaControl((int)equipPara.ParaValueType, equipPara.RefValue, dgv.CurrentCell.Value.ToString());
//get the rectangle of the current cell in the datagridview
rect =
dgv.GetCellDisplayRectangle(dgv.CurrentCell.ColumnIndex, dgv.CurrentCell.RowIndex, false);
//show the control in the rectangle
paraControl.Top = rect.Top;
paraControl.Left = rect.Left;
paraControl.Height = rect.Height;
paraControl.Width = rect.Width;
paraControl.Visible = true;
}
}
//获得不同的编辑器
private void getParaControl(int paraVauleType,string refValue,string currentValue)
{
paraControl = new Control();
switch (paraVauleType)
{
case 0:
decimal inc;
decimal value;
try
{
if (string.IsNullOrEmpty(refValue)) inc = 1;
else inc = Convert.ToDecimal(refValue);
if (string.IsNullOrEmpty(currentValue)) value = 0;
else value = Convert.ToDecimal(currentValue);
paraNumericUpDown.Increment = inc;
paraNumericUpDown.Value = value;
}
catch (Exception ex)
{
log.Debug(String.Format("{0}\t{1}", ex.Source, ex.Message));
return;
}
paraControl = paraNumericUpDown;
if (paraNumericUpDown.Value!=null)
paraControl.Text = paraNumericUpDown.Value.ToString();
break;
default:
break;
}
}
//NumericUpDown值变化事件函数
private void frmTestItem_ValueChanged(object sender, EventArgs e)
{
if (paraControl is NumericUpDown)
myDgvNoAdjInPara.CurrentCell.Value = ((NumericUpDown)paraControl).Value;
}
...全文
相关推荐
WPF自定义的NumericUpDown控件
WPF自定义的NumericUpDown控件,有源码。使用很方便,也可以自己定制。
扩展的NumericUpDown控件
扩展的NumericUpDown控件,具有更好的焦点和鼠标滚轮管理。
AJAX控件NumericUpDown
AJAX控件NumericUpDown 的视频及视频的相关代码
从头开始创建NumericUpDown控件
显示在WPF中创建完整的NumericUpDown控件的整个过程。
DataGridView中添加NumericUpDown控件源码
DataGridView中添加NumericUpDown控件;将dll引用自己的工程文件即可


