DataGridView ImageColumn Handle “Enter” key to perform Click

蹲街弑〆低调 提交于 2020-01-15 10:34:39

问题


How to fire Click event of DataGridViewImageColumn when I press Enter. Currently when I press the Enter key on DataGridViewImageColumn it moves to next cell.

Please help.


回答1:


You can put the code that you want to run in CellContentClick in a method and then on both CellContentClick and KeyDown call that method.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex== 3)
        DoSomething(e.RowIndex, e.ColumnIndex);
}

public void DoSomething(int row, int column)
{
    MessageBox.Show(string.Format("Cell({0},{1}) Clicked", row, column));
}

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    var cell = this.dataGridView1.CurrentCell;
    if (cell != null && e.KeyCode == Keys.Enter &&
        cell.RowIndex >= 0 && cell.ColumnIndex == 3)
    {
        DoSomething(cell.RowIndex, cell.ColumnIndex);
        e.Handled = true;
    }
}


来源:https://stackoverflow.com/questions/38609560/datagridview-imagecolumn-handle-enter-key-to-perform-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!