Link to home
Start Free TrialLog in
Avatar of brokeMyLegBiking
brokeMyLegBiking

asked on

DataGridView - programatically hit the F2 key

In the DataGridView my code is adding a new row, after I do this I would like to go ahead and place the cursor inside the first cell so that the user can start editing its value. How can I do this?

(I want to accomplish the same effect as if the user clicked into the cell or hit the F2 key to begin editing it). I'm trying to save the user from having to take this step.

Also, when a user begins typing a new blank row is autogenerated at the bottom. I want to make sure that this new row appears (otherwise it causes problems for my program).

-Joseph
Avatar of brokeMyLegBiking
brokeMyLegBiking

ASKER

I figured out how to programatically beginEdit on a cell:

            dataGridView1.CurrentCell = dataGridView1.Rows(dataGridView1.Rows.Count - 1).Cells(0);
            dataGridView1.BeginEdit(false);

Now I just have to solve the second part of my question.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That was interesting, but that doesn't qute solve the 2nd issue.

Perhpas if I programmatically send a key to the cell after I put it in edit mode? Is there an easy way to programmatically press a key so that the new row appears? Or is there a better way to make that pesky new row appear?
does this help?

void Grid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
   Grid.FirstDisplayedCell = Grid.Rows[e.RowIndex].Cells[0];
}
try this one

Datagridview1.AllowUserToAddRows = True
I solved the issue, by just setting the allowusertoAddRows to false and adding a button. That takes the uncertainty out of it.

-Joseph