2

我正在开发一个表格datagridview

我想要的结果是:

  • 当用户单击只读单元格时,光标将移动到可编辑单元格。
  • 当用户单击可编辑单元格时,光标将位于此当前可编辑单元格上。

我做这个Cell_Enter Event(我有一些理由在Cell_Enter.我必须使用 Cell_Enter 上编码)。

DataGridViewCell cell = myGrid.Rows[cursorRow].Cells[cursorCol];
myGrid.CurrentCell = cell;
myGrid.BeginEdit(true);

点击就Editable CellOK了,点击就ReadOnly Cell报异常错误:

错误-> 操作无效,因为它会导致对 SetCurrentCellAddressCore 函数的可重入调用。

那么,这个问题有解决方案吗?(当用户单击时ReadOnly Cell,光标将移动到Editable单元格。)

编辑:我想要的解决方案是如何将光标移动到不是当前单元格的其他单元格?

4

3 回答 3

2

我在这里找到了解决这个问题的方法。


      private void myGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            //Do stuff
            Application.Idle += new EventHandler(Application_Idle);

        }

        void Application_Idle(object sender, EventArgs e)
        {
            Application.Idle -= new EventHandler(Application_Idle);
            myGrid.CurrentCell = myGrid[cursorCol,cursorRow];
        }
于 2013-02-07T07:34:45.313 回答
1

尝试使用If.. else ..statement

if (cursorCol == 1) //When user clicks on ReadOnly Cell, the Cursor will move to Editable Cell.
{
   myGrid.CurrentCell = myGrid[cursorRow, cursorCol];
}
else //When user clicks on Editable Cell, the Cursor will be on this Current Editable Cell.
{
  //Do stuff
  myGrid.BeginEdit(true);
}
于 2013-02-07T05:03:54.690 回答
1

我不是 100% 确定这会在你的情况下工作,但由于我们客户的一个愚蠢的 UI 要求,我曾经遇到过这样的事情。快速修复是将代码包装在BeginInvoke. 例如:

BeginInvoke((Action)delegate
{
    DataGridViewCell cell = myGrid.Rows[cursorRow].Cells[cursorCol];
    myGrid.CurrentCell = cell;
    myGrid.BeginEdit(true);
});

本质上,这将使它在CellEnter事件发生后执行代码,允许DataGridView它在导致异常的幕后执行任何操作。

最终,它被重构为一个自定义控件,该控件可以扩展DataGridView并且BeginInvoke不再需要。

于 2013-02-07T18:02:26.313 回答