在DataGridView的编辑模式下

在DataGridView的编辑模式下

本文介绍了在DataGridView的编辑模式下,如何使用默认功能删除键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在电子表格程序中,我目前具有以下功能:当用户在单元格上按Delete键时,它将单元格的值设置为空字符串;但是,当用户在编辑单元格时按Delete键时,Delete键的默认功能不起作用。我当前对Delete键的实现如下:

In my spreadsheet program, I currently have functionality for when the user presses the delete key on a cell, it sets the cell's value to an empty string; however, when the user presses delete while editing a cell, the default functionality of the delete key does not work. My current implementation for the delete key is as follows:

private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (false == dataGridView1.CurrentCell.IsInEditMode)
    {
        foreach (DataGridViewCell selected_cell in dataGridView1.SelectedCells)
        {
            Cell change_cell = _workbook.CurrentSpreadsheet.GetCell(selected_cell.RowIndex, selected_cell.ColumnIndex);

            // The text is not null nor empty
            if (false == string.IsNullOrEmpty(change_cell.Text))
            {
                change_cell.Text = "";
            }
        }
    }
    // The user is editing a cell
    else
    {
        // When the user presses delete, use the default functionality
        // as in remove one character each press.
    }
}

对于我的else语句,是否有启用方法编辑单元格时删除键的默认功能?

For my else statement, is there a way to enable the delete key's default functionality while editing a cell?

推荐答案

尝试一下,它可以正常工作。

Try this, it is working as expected.

private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
    DeleteCellsIfNotInEditMode();
}

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Delete)
    {
        DeleteCellsIfNotInEditMode();
    }
}

private void DeleteCellsIfNotInEditMode()
{
    if (!dataGridView1.CurrentCell.IsInEditMode)
    {
        foreach (DataGridViewCell selected_cell in dataGridView1.SelectedCells)
        {
            selected_cell.Value = "";
        }
    }
}

这篇关于在DataGridView的编辑模式下,如何使用默认功能删除键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:41