我在 datagridview 中遇到了问题。 I have done some code in keydown event for changing the tab focus but when tab reaches last of column it gives a error
"Current cell cannot be set to an invisible cell".
我让最后一个单元格不可见,因为我不想让那个单元格可见。
我在KeyDown事件中写了下面的代码
private void m3dgvDepositDetails_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Tab && notlastColumn)
{
e.SuppressKeyPress = true;
int iColumn = m3dgvDepositDetails.CurrentCell.ColumnIndex;
int iRow = m3dgvDepositDetails.CurrentCell.RowIndex;
if (iColumn == m3dgvDepositDetails.Columns.Count - 1)
m3dgvDepositDetails.CurrentCell = m3dgvDepositDetails[0, iRow + 1];
else
m3dgvDepositDetails.CurrentCell = m3dgvDepositDetails[iColumn + 1, iRow];
}
}
catch (Exception ex)
{
CusException cex = new CusException(ex);
cex.Show(MessageBoxIcon.Error);
}
}
最佳答案
错误是不言自明的:您将 CurrentCell
设置为不可见的单元格并且它是被禁止的,这意味着单元格的行 或 单元格的列是隐。为避免这种情况,请不要在设置 CurrentCell
之前隐藏行/列或检查 Visible
属性。
如果问题是您应该使用的最后一列:
private void m3dgvDepositDetails_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Tab && notlastColumn)
{
e.SuppressKeyPress = true;
int iColumn = m3dgvDepositDetails.CurrentCell.ColumnIndex;
int iRow = m3dgvDepositDetails.CurrentCell.RowIndex;
if (iColumn >= m3dgvDepositDetails.Columns.Count - 2)
m3dgvDepositDetails.CurrentCell = m3dgvDepositDetails[0, iRow + 1];
else
m3dgvDepositDetails.CurrentCell = m3dgvDepositDetails[iColumn + 1, iRow];
}
}
catch (Exception ex)
{
CusException cex = new CusException(ex);
cex.Show(MessageBoxIcon.Error);
}
}
关于c# - 当前单元格不能设置为datagridview中的不可见单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18018235/