Question

I have a datagridview that accepts a list(of myObject) as a datasource. I want to add a new row to the datagrid to add to the database. I get this done by getting the list... adding a blank myObject to the list and then reseting the datasource. I now want to set the focus to the second cell in the new row.

To CLARIFY i am trying to set the focus

Was it helpful?

Solution

You can set the focus to a specific cell in a row but only if the SelectionMode on the DataGridView is set to CellSelect. If it is, simply do the following:

dataGridView.Rows[rowNumber].Cells[columnNumber].Selected = true;

OTHER TIPS

In WinForms, you should be able to set the

Me.dataEvidence.SelectedRows

property to the row you want selected.

In Visual Studio 2012 (vb.NET Framework 4.50), you can set the focus on any desired cell of a DataGridView control.

Try This:

Sub Whatever()

    ' all above code

    DataGridView1.Focus()
    DataGridView1.CurrentCell = DataGridView1.Rows(x).Cells(y)       'x is your desired row number, y is your desired column number

    ' all below code

End Sub

Okay, that works for me. I hope that it works for you, too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top