根据设计要求,用户无法直接编辑datagridview . 它处于只读模式 . 当用户双击单元格时,datagridview的只读属性变为false,单元格接受键盘输入 . 但是,原始键盘输入需要在进入单元格之前进行格式化 . 所以,我拦截KeyPress事件如下:
private void dgw_keyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
此时,单元格处于编辑模式和脏模式 . 然后我以不同的方法更新Value属性并调用 dgw.Refresh()
,它应该在单元格上显示更新的值 . 但事实并非如此 . 它只会在当前单元格不脏并且不处于编辑模式时更新 . 如何在单元格仍处于编辑模式时强制显示更新后的值?
有任何想法吗?
4 回答
我用下面的代码解决了 .
使用下面的内容刷新当前单元格的值,更改为适合您的EditingControl类型
另一种方法:
您可以通过实现IDataGridViewEditingControl接口来实现 . 我认为's the way to get the most control over how the cell enters and leaves edit mode. You can find more details in section 5.11 of Mark Rideout' s DataGridView FAQ (DOC)
试试DataGridView.EndEdit方法 .