views:

253

answers:

1

I have a databound dataGridView. When I click on the new row the DefaultValuesNeeded event is fired and the row is populated with my default values. If I then hit escape, the new row will be deleted. How can I achieve the same behavior from a button click event handler? That is, the click event should mimic what occurs by clicking on the new row and the escape key should cause the new row to be deleted as well.

A: 

To mimic the ESC key add the following SendKeys call inside the button click event

SendKeys.Send("{ESC}");

To add a row to a databound DataGridView, add the row to the underlying data source (e.g., DataTable or BindingSource) then refresh the grid. Ex:

dataSet.Tables("yourtablename").Rows.Add();
SwDevMan81
Perhaps I wasn't clear. The click event needs to add a new row to the dataGridView, just as clicking on the new row in the dataGridView does. A subsequent escape keypress should then delete the newly added row.
ejwipp
This adds a row, but does not result in a call to DefaultValuesNeeded, nor can the new row be removed by hitting the escape key.
ejwipp
Right, you would add your defaults in the "Add()" method. Then you would listen for the escape key and call dataSet.Tables("yourtablename").Rows.RemoveAt();
SwDevMan81