本人新手,问个菜鸟问题
在winform中使用dataGridView,在输入完某个cell的内容,离开时,想判断其内容,如不符合要求,要求光标重新强制回到此cell,说白了就是要让这个格子不填出符合要求的数据就让它无法跳到其它格子去我在CellLeave事件中写了:
dataGridView.CurrentCell = dataGridView.Rows[当前行].Cells[当前列];这条语句本身应该没有问题啊,可以让dataGridView任何一个格子获取焦点但是实际执行起来,为什么就无效了呢,感觉上鼠标点击或TAB后,焦点还是到另一个格子里去了,不会重新回到这个格子

解决方案 »

  1.   



    dataGridView.CurrentCell = dataGridView.Rows[当前行].Cells[当前列];
    加上下面的代码:
    dg.EditMode = DataGridViewEditMode.EditProgrammatically;//.EditOnKeystroke;//.EditOnEnter;
    dg.BeginEdit(true);
      

  2.   

    回楼上的,不行啊
    是不是这个不应该写在CellLeave事件中呢?那应该在哪处理呢?
      

  3.   

    以下为要求不能为空的例子private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        if (dataGridView1.Columns[e.ColumnIndex].Name == "colName") 
        { 
            if (String.IsNullOrEmpty(e.FormattedValue.ToString())) 
            { 
                dataGridView1.Rows[e.RowIndex].ErrorText = "此列值不能为空!"; 
                e.Cancel = true; 
            } 
        }
    }
    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
    }