Ben
Ben

Reputation: 3912

Setting a DatagridView row to have focus on Form Load

I have a dialog that containers a DataGridView, this dialog is opened from a parent Form. I would like the DataGridView to have focus as soon as the form is opened, so on pressing the down key would enable you to scroll down through the rows straight away. I have tried setting the tab index so that the DataGridView is the first selected item in the form. I have also tried calling the DataGridView.Focus() method in both the dialog constructor and the Form_Load event. I have also tried setting the

DataGridView.Rows(0).Selected = True

None of these seem to work.
Does anyone know how I could fix this problem?

Thanks,
Ben

Upvotes: 2

Views: 5028

Answers (2)

Ben
Ben

Reputation: 3912

I have found the answer. It seems that DataGridView.Select() does the trick of focusing the grid.

Upvotes: 4

Danail
Danail

Reputation: 10583

I assume that you open your dialog with ShowDialog() method.

It worked for me when I wrote this in the Form_load method:

dataGridView1.TabIndex = 0;

I guess that won't work for you (since you said you tried).

Maybe you have some other control with tabindex = 0, that gets in the way?

Upvotes: 0

Related Questions