c# DataGridView EditMode and DeleteKey

c# winforms

My DataGridView has:

SelectionMode - CellSelect

EditMode - EditOnKeyStroke

It works well for all Keys except fot DeleteKey (when pressed - nothing happend).

I want - if DeleteKey is pressed - selected Cell (or Cells) stay selected, but - empty value (like in Excel).

Must I create a separate KeyDownEvent for DeleteKey only ?


The delete key has special significance in (potentially) allowing the user to delete a row from the grid. The Delete key is not permitted to initiate 'Edit' mode in the DataGridView. So, you need to get the cell into Edit mode somehow in order to allow the delete key to be handled by the DataGridViewCell (instead of the DataGridView itself).

To do this, you could either implement a KeyPress handler as you suggest, so that pressing delete gets you into edit mode, before being handled again to delete the content. Or, if that's too much effort you can also change the DataGridView.EditMode to 'OnEnter', in which case each cell is always put into edit mode upon gaining focus, either by mouse or tabbing between cells. Then, pressing delete will cause the contents to be deleted.

链接地址: http://www.djcxy.com/p/62256.html

上一篇: MS Excel在复制单元格时添加换行符

下一篇: c#DataGridView EditMode和DeleteKey