Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Retrieving last row of databound DGV?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] [2005] Retrieving last row of databound DGV?

    I have a DataGridView that is bound to a dataset. What I wish to do is when the grid is loaded...I want the last row to be the selected row. I also need to declare an instance of the row but not sure how to reference the last row.

    This is what I have currently.

    Code:
             Dim row As DataGridViewRow = grdStatus.CurrentRow
    I need to have something like this:

    Code:
             Dim row As DataGridViewRow = grdStatus.LastRow
    Thanks,
    Blake

  2. #2
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    351

    Re: [2005] Retrieving last row of databound DGV?

    Off the top of my head i would say something like:

    vb Code:
    1. Dim row As DataGridViewRow = grdStatus.rows(grdStatus.rowCount)

    the row index might be zero-indexed so you might have to use grdStatus.rowcount-1. Sorry didn't check.
    Rico

    Using: VB.net & MS SQL

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Retrieving last row of databound DGV?

    You have a datatable that is bound to your DGV. So how many rows there are in the datatable, that many rows will be shown on the DGV (not including the last bank row if you set the DGV's property AllowUserToAddRows = True).
    Now, datatable class has the Rows property which returns a DataRow collection. Collections have the Count property to tell you how many items exists in a collection. You just have to get this count, and set the CurrentCell of the DGV points to a cell in the (count -1) row, which is the last row on the DGV. Put it all in code:

    Code:
    'Counting the rows in myDataTable
    Dim rowCount As Integer = myDataTable.Rows.Count
    
    'Set the 1st cell of the last row in DGV as the current cell
    myDGV.CurrentCell = myDGV.Item(rowCount -1, 0)
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: [2005] Retrieving last row of databound DGV?

    This is what I came up with from just trying different things:

    Code:
           Dim idx As Integer = grdStatus.Rows.GetLastRow(DataGridViewElementStates.None)
           Dim row As DataRow = dsStatus.Tables("tblStatus").Rows(idx)
           strStatusDescription = row("status")
    I tested it and it works like its supposed to.
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width