You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

设置DataGridView.CurrentCell后无法在目标单元格输入内容的问题求助

解决方案

出现该问题的核心原因是DataGridView重新加载数据后UI渲染、焦点切换的时序冲突,你之前的设置逻辑执行时控件还未完成状态重置,导致编辑状态激活失效,可按以下步骤修复:

  1. 调整定位逻辑的执行时序
    将定位和激活编辑的逻辑放到UI线程异步队列中,等DataGridView完成数据绑定、渲染的全部操作后再执行,同时额外增加DataGridView的焦点设置(这步是最容易遗漏的):
int iColumn = e.ColumnIndex;
int iRow = e.RowIndex;

dgvDataGrid.BeginInvoke(new Action(() => 
{
    // 先确保DataGridView获得焦点
    dgvDataGrid.Focus();
    // 设置当前单元格
    dgvDataGrid.CurrentCell = dgvDataGrid[iColumn + 1, iRow];
    dgvDataGrid.CurrentCell.Selected = true;
    // 激活编辑状态
    dgvDataGrid.BeginEdit(true);
}));
  1. 修正DateTimePicker退出时的焦点逻辑
    在DateTimePicker的选择完成、关闭事件中主动将焦点交还给DataGridView,避免焦点残留导致编辑激活失败:
// 示例:DateTimePicker的ValueChanged事件中增加
dgvDataGrid.Focus();
  1. 检查控件基础配置
    确认DataGridView的编辑模式配置符合预期,建议将EditMode属性设置为EditOnEnter,选中单元格即自动进入编辑状态:
dgvDataGrid.EditMode = DataGridViewEditMode.EditOnEnter;
  1. 极端情况强制触发编辑
    如果上述操作仍未生效,可以额外模拟编辑快捷键触发编辑:
// 在BeginEdit之后追加
SendKeys.Send("{F2}");

内容的提问来源于stack exchange,提问作者DBAStarter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 12:36:04