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?
 
Jorge PaulinoConnect With a Mentor IT 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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
All Courses

From novice to tech pro — start learning today.