|
-
Oct 30th, 2008, 10:46 AM
#1
Thread Starter
PowerPoster
[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,
-
Oct 30th, 2008, 12:33 PM
#2
Hyperactive Member
Re: [2005] Retrieving last row of databound DGV?
Off the top of my head i would say something like:
vb Code:
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
-
Oct 30th, 2008, 12:45 PM
#3
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 -
-
Oct 30th, 2008, 01:04 PM
#4
Thread Starter
PowerPoster
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|