【发布时间】:2013-09-18 14:19:48
【问题描述】:
我有两个 datagridview 列; startdate 和 enddate 使用本文描述的自定义 MaskedTextBox 列类型:http://www.codeproject.com/Articles/26005/DataGridViewColumn-Hosting-MaskedTextBox
现在,当用户在列中输入有效日期时,我想将编辑焦点自动移动到同一行的下一个单元格。
这个 MaskedTextBox 列的工作方式是在 OnTextChanged() 事件中 我尝试将 .Text 值转换为 DateTime,如果这种情况毫无例外地发生,我将该 DateTime 值分配给 DataGridView.CurrentCell.Value。
然后我尝试使用 DataGridView.CellValueChanged 事件移动到下一个单元格,如下所示:
private void myDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == -1 || e.RowIndex == -1)
return;
//Move to next cell on the same row
if (myDataGridView.Columns[e.ColumnIndex].ValueType.Equals(typeof(DateTime)))
{
try
{
myDataGridView.EndEdit(DataGridViewDataErrorContexts.Commit);
int NewColIndex = myDataGridView.CurrentCell.ColumnIndex + 1;
if (NewColIndex > -1 && NewColIndex < myDataGridView.Columns.Count)
myDataGridView.CurrentCell = myDataGridView[NewColIndex, myDataGridView.CurrentCell.RowIndex];
}
catch
{
}
}
}
编辑焦点确实会更改到下一列/单元格,但是当我开始在第二列/单元格中输入时,我得到一个 NullReference 异常,它源自 PositionEditingControl() 中的方法i>DataGridViewMaskedTextCell 类。
谁能告诉我为什么会这样?
有没有比设置 .CurrentCell 移动到下一个单元格的“更好”方式?
编辑: 似乎这个问题与第二列也使用这个自定义编辑控件的事实有关。用常规的 TextBoxColumn 作为第二列尝试了同样的事情,效果很好。我猜第二列初始化其编辑控件的方式有什么问题?
【问题讨论】:
-
发送tab键呢?
-
发送 Tab 键 = 相同结果
标签: c# .net winforms datagridview