I have a datagridview on the form.
and there are 4 columns in the grid – No, Answer, Key, Result.
No, Key and Result Columns are read only.
it means that i’m not going to enter some data to these columns.
But i will enter values to Answer column.
And values can be only – A, B, C, D and E.
when i press for example X key i dont want X to be show in the cell.
i can do it with textbox’s with keypress event.
now can you tell me, how can i use keypress event of Answer columns cell event to realize it.
You can use the
EditingControlShowingevent of theDataGridViewto accomplish this.In the eventhandler of this event, you have access to the
Textboxthat ‘s being displayed whenever you enter data inside the datagrid.This means, that at this point, you can attach an eventhandler to the
KeyPressevent of the textbox that is displayed:Since the DataGridView will always ‘share’ the textbox control for every cell in the grid that uses textboxes, you should check whether it is the first time that the event is raised.
If you have other columns in the DataGridView which are not readonly, and where you want the user to enter data as well, which is not constrained, then this approach won’t be that suitable. (Unless you check in the
OnKeyPresseventhandler to which column the current cell belongs).(Note that you’ll have to consider the lowercase a, b, c as well.