0

我试图DataGridViewCell仅在选择模式而不是在 C# 中的编辑模式中检测离开事件。那就是下面给出的代码在这里显示:

      private void dgv_CellLeave(object sender, DataGridViewCellEventArgs e)
      {
              if (dgvC.CurrentCell.ColumnIndex == 0)
              {
                  if (dgv.CurrentCell.Value == null)
                      MessageBox.Show("You have to enter somthing");
              }
      }

    private void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
              if (dgv.CurrentCell.ColumnIndex == 0)
              {
                  if (dgv.CurrentCell.Value.ToString() !="S" )
                      MessageBox.Show("You have to enter S");
              }

 }

当我选择网格单元格时,上述事件正常工作,但在编辑单元格时它们不起作用。意味着在这两种情况下都发生了休假事件。所以我想检测当前单元格是否处于编辑模式或选择模式,之后我必须将光标放在同一个单元格中,不应更改。谁能告诉我该怎么做?

4

2 回答 2

1

你好那里尝试使用这个事件。

    private void dataGridView2_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        if (dataGridView2.IsCurrentCellDirty)
            if (e.ColumnIndex == 0)
            {
                if (string.IsNullOrWhiteSpace(dataGridView2[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString()))
                {
                    e.Cancel = true;
                    MessageBox.Show("Please enter some text before you leave.");
                }
                else if (dataGridView2[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString() != "S")
                {
                    e.Cancel = true;
                    MessageBox.Show("You have to enter S");
                }
            }
    }
}
于 2012-12-16T12:28:57.327 回答
0

在单元格编辑模式下不要使用 CurrentCell 只需使用“e”参数来查找单元格。

示例代码:

private void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        if (dgv[e.ColumnIndex, e.RowIndex].Value.ToString() !="S" )
        {
            MessageBox.Show("You have to enter S");
        }
    }
}
于 2012-12-16T09:15:54.127 回答