Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
With my Form_Load() I wish to set my 150'th Row as my TopRow, while the DataGridView gets visible. For that when I setup the currentcell Property its working fine. no prolem.
But when I try through the FirstDisplayedScrollingRowIndex() property its not working.

Its Not Working

C#
dataGridView1.Rows[150].Selected = true;
dataGridView1.FirstDisplayedScrollingRowIndex = 150;
dataGridView1.Focus();


But the below is working

C#
dataGridView1.Rows[150].Selected = true;
dataGridView1.CurrentCell = dataGridView1.Rows[150].Cells[0];
dataGridView1.Focus(); 


Thanks For The Helps
Posted
Updated 2-Jan-20 21:40pm

You can't do this in the form load event because not everything is setup until that event completes. Specifically, the visual elements are not all composed at the time you are trying to set the scrolling row index. I'm guessing you are getting an invalid operation or a null reference exception.

The trick I've used before is to decouple the selection from the form load event handler. In your form class, add a timer. (If you are doing this in Silverlight or WPF, add a Storyboard) In the Form_Load event handler, set the time to 1 second or the Storyboard to one frame and then start it on the way out of the form load event handler. (make it the last call)

In the event handler for the timer or storyboard, you should be able to set the row index. If you have to use a timer, remember that the timer events happen on a different thread and you have to marshal them to the UI thread by invoking a delegate. (There are examples of that of CodeProject) The storyboard in SL & WPF will automatically marshal that call so you don't have to worry about it.

The result is that just after your form loads and becomes visible, the timer or storyboard will fire and set your row index for you.
 
Share this answer
 
v2
Comments
paultraite 4-Mar-15 12:07pm    
I think you can also do this in the Form.Shown event
FirstDisplayedScrollingRowIndex 
working for me
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900