views:

428

answers:

2

Hi,
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

A: 

Hi,

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?

Danail
Hi Danail, no this hasn't worked for me. I have checked that there is nothing else that is having the tabindex set at runtime as well, and nothing. I have also checked that there is no other .Focus() being called. Has this allowed you to be able to scroll through rows using the up and down keys without having to selected the grid first with the mouse then? Thanks
Ben
Yes, I was able to scroll down immediately with the keyboard arrows.There was problem only when other component had tabindex = 0 (it wasn't set programatically, I set it in the Properties window). I assume that you have something with tabindex = 0 (maybe the first compoenent you have dropped on the form).
Danail
+2  A: 

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

Ben