怎么样让 DataGridView 单元格获得焦点并且是编辑状态,比如现在焦点在第一行的第二列,当单击第一行的第三列时,让第一行第一列获得焦点,并且是编辑状态

解决方案 »

  1.   

    编辑列?还是行?gridView好像是编辑行吧
      

  2.   

    有具体的代码吗? 我是想实现excel的功能。
    在一个新行获得编辑状态,如果当前行的第一列没有值 就把编辑状态跳到当前行的第一列,我试了好像只能让第一列选中而不能处于编辑状态
      

  3.   

    LZ试试将DGV的EditMode设置成EditOnEnter,再在SelectionChanged事件里添加以下代码:
    if (DGV.SelectedCells.Count != 0)
    {
          if (DGV.SelectedCells[0].ColumnIndex == 2)
          {
               DGV.CurrentCell = DGV.Rows[DGV.SelectedCells[0].RowIndex].Cells[0];
           }
    }
      

  4.   


     private void initdata()
            {
                DataTable tbl = new DataTable();
                tbl.Columns.Add("id", typeof(string));
                tbl.Columns.Add("name", typeof(string));
                for (int i = 0; i < 3; i++)
                {
                    DataRow r = tbl.NewRow();
                    r[0] = "";
                    r[1] = i.ToString();
                    tbl.Rows.Add(r);
                }
                this.dataGridView1.DataSource = tbl.DefaultView;
                this.dataGridView1.CellEnter += new DataGridViewCellEventHandler(dataGridView1_CellEnter);
            }        void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex == 0 && e.ColumnIndex == 0)
                {
                    if (this.dataGridView1.CurrentCell.Value.ToString() == "")
                    {
                        this.dataGridView1.BeginEdit(false);
                    }
                }
            }
      

  5.   

    你有没有将DataGridView的EditMode属性设置成EditOnEnter呢?
      

  6.   

    已经设置了,按你的代码只能让第一列处于选中的状态而不是编辑状态。我也是了后面加beginEdit()方法但是还不行,结果是第一列选中,而以前选的列处于编辑,我也正奇怪着呢!
      

  7.   

    你的DGV或者对应的列是否是设置为只读的呢??如果不是的话就应该会没有问题的,因为设置为EditOnEnter后就没有的区分开选中状态与编辑状态了,只要焦点落在cell上就自动变成编辑状态了,如果设置为只读的话,就只有选中状态没有编辑状态了
      

  8.   

    DGV.CurrentCell =
    EditOnEnter=
    oneditcontrolshowing写代码
    都用上,试试(我没试过。)