WinForm如何将光标定位在DataGridView的某个单元格上

keconghua 2008-04-28 09:10:09
我们可以通过键盘上的TAB键或左右键来指定到DataGridView的某个单元格
上下左右的移动,这里我会触发一个事件:DataGridView1_CellLeave事件
当执行出错时再返回上一个单元格,不知道如何实现
用:this.dg_DataGridView1.Rows[e.RowIndex].Cells[3].Selected = true;
这条语句好像不起作用,谢谢高手指点!
...全文
2073 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
ltlx 2008-05-09
  • 打赏
  • 举报
回复
this.dataGridView1.CurrentCell = this.dataGridView1[3, 0];
this.dataGridView1.BeginEdit(true);
Alamiye 2008-05-08
  • 打赏
  • 举报
回复
我非常了解楼主遇到的问题,这也是我曾经做WINFORM开发时提出过的问题,我可以很负责任地告诉楼主如果不采用第三方控件或重写控件的话是无法达到楼主想要的效果的!
话又说回来,楼主如果为了达到这个效果要花很多时间的话我认为是不划算的!我当时的做法就是在信息框弹出后让单元格为选中状态!
keconghua 2008-05-08
  • 打赏
  • 举报
回复
只是为选中状态是没有问题,但是光标却到了下一个单元格,先中的单元又是原来的单元格,不同步呀
呵呵,一直没有想到什么好方法

哪里有第三方控件呀,有的话,哪个兄弟能否共享一下,谢谢!
keconghua 2008-05-06
  • 打赏
  • 举报
回复
其实我的意思很简单:
就是当执行下面的提示信息时,将光标返回到DataGridView中指定的单元格中
MessageBox.Show("提示:物料代码不存在,请从新输入", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
keconghua 2008-04-29
  • 打赏
  • 举报
回复
this.dg_Trans.CurrentCell = new DataGridCell(e.RowIndex, 3);
但VS2005下的DataGridView在new 时找不到:DataGridViewCell

怎么实现呢,高手指点呀,谢谢
keconghua 2008-04-29
  • 打赏
  • 举报
回复
还是没有搞定
我把我所有的代码贴出来:
private void dg_Trans_CellLeave(object sender, DataGridViewCellEventArgs e)
{
try
{
this.dg_Trans.EndEdit();
if (e.RowIndex == -1) return;
if (e.RowIndex < 0 || e.ColumnIndex < 0) return;
if (e.ColumnIndex != 3)
{
DataSet dsItem = new DataSet();
DataAccess.DAFittings Fitting = new DataAccess.DAFittings();
if (this.dg_Trans.Rows[e.RowIndex].Cells[3].Value != null)
{
dsItem = Fitting.GetMaterial(this.dg_Trans.Rows[e.RowIndex].Cells[3].Value.ToString());
if (dsItem.Tables[0].Rows.Count == 1)
{
this.dg_Trans.Rows[e.RowIndex].Cells[2].Value = dsItem.Tables[0].Rows[0][0].ToString();
this.dg_Trans.Rows[e.RowIndex].Cells[4].Value = dsItem.Tables[0].Rows[0][2].ToString();
}
else if (dsItem.Tables[0].Rows.Count == 0)
{
MessageBox.Show("提示:物料代码不存在,请从新输入", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
//this.dg_Trans.ClearSelection();
//this.dg_Trans.Rows[e.RowIndex].Cells[3].Selected = true;//光标返回到错语代码列
this.dg_Trans.EditMode = DataGridViewEditMode.EditOnEnter;
this.dg_Trans[e.RowIndex, 3] = this.dg_Trans[e.RowIndex, e.ColumnIndex-1];
//this.dg_Trans.CurrentCell = this.dg_Trans[e.RowIndex, 3];
//this.dg_Trans.BeginEdit(true);
}
}
}

}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
------------------------------------------------------------------------------
是用TAB键来实现,最好是能用ENTER键来实现像TAB键一样的效果
这里会去做检查,如果物料不存在即提示,确认后返回到刚才有问题的那个单元格!
wumingbing_8027 2008-04-29
  • 打赏
  • 举报
回复
1\this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
this.dataGridView1.CurrentCell = this.dataGridView1[1, 0];
2\this.dataGridView1.CurrentCell = this.dataGridView1[1, 0];
this.dataGridView1.BeginEdit(true);

dataGridView1[1, 0] = dataGridView1[ColumnIndex, RowIndex],这个和习惯不一样
第二种方法不能放在form_onload里
wumingbing_8027 2008-04-28
  • 打赏
  • 举报
回复
dataGrid.CurrentCell = new DataGridCell(RowIndex, ColumnIndex);
这个怎么会报错呢

报错?我用的是2005
错误贴出来
shuizhizhou0007 2008-04-28
  • 打赏
  • 举报
回复
关注
keconghua 2008-04-28
  • 打赏
  • 举报
回复
this.dataGridView1.ClearSelection();
this.dataGridView1.[e.RowIndex].Cells[3].Selected = true;
这个我试了还是不起作用呀

dataGrid.CurrentCell = new DataGridCell(RowIndex, ColumnIndex);
这个怎么会报错呢

继续关注。。。。。。

机器人 2008-04-28
  • 打赏
  • 举报
回复
DataGridViewCell 而非 DataGridCell!

wumingbing_8027 用的是DataGrid。
wumingbing_8027 2008-04-28
  • 打赏
  • 举报
回复
dataGrid.CurrentCell = new DataGridCell(RowIndex, ColumnIndex);
h_w_king 2008-04-28
  • 打赏
  • 举报
回复
this.dataGridView1.ClearSelection();
this.dataGridView1.[e.RowIndex].Cells[3].Selected = true;
keconghua 2008-04-28
  • 打赏
  • 举报
回复
我这也是2005
我是这样写的:
this.dg_Trans.CurrentCell = new DataGridCell(e.RowIndex, 3);
编译里出错:
错误:无法将类型“System.Windows.Forms.DataGridCell”隐式转换为“System.Windows.Forms.DataGridViewCell”

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧