get datagridview to show last record instead of first.

Hi,
I was wondering is it possible to get the DATAGRID VIEW to show the last record instead of the first when it is initially loaded.

I am using VB.net with a Windows Form to display a table from Access 2003.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DataSet2.tblBeverages' table. You can move, or remove it, as needed.
        Me.TblBeveragesTableAdapter.Fill(Me.DataSet2.tblBeverages)

Open in new window

billyboy71Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jorge PaulinoIT Pro/DeveloperCommented:
Hi billyboy71,

Ho do you define last record ? Do you have any Id, date, etc ?

You can sort the datagridview to do that after you fill it.
0
billyboy71Author Commented:
Hi,
Well, the last record in terms of the last row?
I was thinking about using FirstDisplayedScrollingRowIndex , but I can't seem to get it to work.

Peter
0
Jorge PaulinoIT Pro/DeveloperCommented:
FirstDisplayedScrollingRowIndex  is just the first row to appear/displays in the datagridview. You can do :
DataGridView.FirstDisplayedScrollingRowIndex  = 3

Where 3 is the index. If you want the last number you can use:
DataGridView.FirstDisplayedScrollingRowIndex  = DataGridView.RowCount -1

But this does not select the record. To select the record you can use
Me.DataGridView.Rows(3).Selected = True


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

billyboy71Author Commented:
Hi,
Thanks for the reply.  I tried the suggestions above. But I can't seem to get the arrow pointer to go to the particular row when the datagridview is loaded .  Is there some execute command that I need to carry out?

Peter
0
billyboy71Author Commented:
Hi,

Thanks for your reply.  Now , I can get the arrow pointer to scroll to the button , but only when I put the the command in a particular method such as when I put the command in a button click.

But I want it to got to that row when the form appears.

Peter
0
billyboy71Author Commented:
Hi,

Is there some kind of method for the Form or Tab, Such as Form.Onfocus or Tab.Onfocus so that it can execute the method of DataGridView.FirstDisplayedScrollingRowIndex  = 3 ?

Otherwise, the form just won't execute it unless I put it in a button on_click method or somethign.

peter
0
billyboy71Author Commented:
Hi,

This is strange it can't carry out that command on the FORM LOAD event. But I can do it on the Button Click event.

Me.DataGridView2.FirstDisplayedScrollingRowIndex = Me.DataGridView2.RowCount - 1

Peter

0
Jorge PaulinoIT Pro/DeveloperCommented:
Hi Peter,

This selects the record and show the arrow pointer.
Me.DataGridView.Rows(3).Selected = True

Can you show your code that you are trying to use ? If you are using this on form load maybe it's to quick and doesn't see anything. Try to do this way or you can add a timer to delay this action:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Me.TblBeveragesTableAdapter.Fill(Me.DataSet2.tblBeverages)
       Applications.DoEvents()
       Me.DataGridView2.FirstDisplayedScrollingRowIndex = Me.DataGridView2.RowCount - 1
End Sub
0
billyboy71Author Commented:
Hi Ipaulino,

I found out what my problem was. The first code that you gave works now. The problem was that my Datagridview was inside a Tab Control. So in order to get it to work I had to put the code in the Tab Control event.

Thanks a lot.

Peter



 Private Sub TabControl1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.Click
        Me.DataGridView2.FirstDisplayedScrollingRowIndex = Me.DataGridView2.RowCount - 1
    End Sub

Open in new window

0
Jorge PaulinoIT Pro/DeveloperCommented:
>> The problem was that my Datagridview was inside a Tab Control. So in order to get it to work I had to put the code in the Tab Control event.
It happens allot! :-)

Glad I could help and thank for the grade!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.