我们可以通过键盘上的TAB键或左右键来指定到DataGridView的某个单元格
上下左右的移动,这里我会触发一个事件:DataGridView1_CellLeave事件
当执行出错时再返回上一个单元格,不知道如何实现
用:this.dg_DataGridView1.Rows[e.RowIndex].Cells[3].Selected = true;
这条语句好像不起作用,谢谢高手指点!

解决方案 »

  1.   

    this.dataGridView1.ClearSelection();
     this.dataGridView1.[e.RowIndex].Cells[3].Selected = true; 
      

  2.   

    dataGrid.CurrentCell = new DataGridCell(RowIndex, ColumnIndex);
      

  3.   

    this.dataGridView1.ClearSelection(); 
    this.dataGridView1.[e.RowIndex].Cells[3].Selected = true;  
    这个我试了还是不起作用呀dataGrid.CurrentCell = new DataGridCell(RowIndex, ColumnIndex);
    这个怎么会报错呢继续关注
      

  4.   

    dataGrid.CurrentCell = new DataGridCell(RowIndex, ColumnIndex);
    这个怎么会报错呢 报错?我用的是2005
    错误贴出来 
      

  5.   

    我这也是2005
    我是这样写的:
    this.dg_Trans.CurrentCell = new DataGridCell(e.RowIndex, 3);
    编译里出错:
    错误:无法将类型“System.Windows.Forms.DataGridCell”隐式转换为“System.Windows.Forms.DataGridViewCell”
      

  6.   

    DataGridViewCell 而非 DataGridCell! wumingbing_8027 用的是DataGrid。
      

  7.   

    this.dg_Trans.CurrentCell = new DataGridCell(e.RowIndex, 3);
    但VS2005下的DataGridView在new 时找不到:DataGridViewCell怎么实现呢,高手指点呀,谢谢
      

  8.   

    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里
      

  9.   

    还是没有搞定
    我把我所有的代码贴出来:
    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键一样的效果
    这里会去做检查,如果物料不存在即提示,确认后返回到刚才有问题的那个单元格!
      

  10.   

    其实我的意思很简单:
    就是当执行下面的提示信息时,将光标返回到DataGridView中指定的单元格中
    MessageBox.Show("提示:物料代码不存在,请从新输入", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      

  11.   

    我非常了解楼主遇到的问题,这也是我曾经做WINFORM开发时提出过的问题,我可以很负责任地告诉楼主如果不采用第三方控件或重写控件的话是无法达到楼主想要的效果的!
    话又说回来,楼主如果为了达到这个效果要花很多时间的话我认为是不划算的!我当时的做法就是在信息框弹出后让单元格为选中状态!
      

  12.   

    this.dataGridView1.CurrentCell = this.dataGridView1[3, 0];
                        this.dataGridView1.BeginEdit(true);