datagridview how to get value from the cell of last row

mrrpotatoo

New member
Joined
Oct 31, 2013
Messages
1
Programming Experience
Beginner
Hello, I'm newbie here.
I'm facing a problem.
How to get the value from the cell of last row?
data.png

i want get the 4 into the textbox, can anyone teach me how to do it?
thanks
 
The Item property of the grid accepts a column index and a row index and returns the cell at that location. Alternatively, the Item property of the Rows collection accepts a row index and returns the row at that index and then the Item property of the Cells collection of that row accepts a column index and returns the cell at that index. Note that Item is the default property in each case so you can actually omit the property name, which is the norm.

Once you have the cell, you get its contents from its Value property. That's returned as an Object reference because it can be any type of object, so you'll need to cast it as the appropriate type. In your case, it might be an Integer or it might be a String but you want it as a String to display in the TextBox anyway so you'd use CStr.
 
Hi,

Just to add to what has already been posted you can get the Index of the Last Row in the DataGridView in a couple of ways. i.e:-

Dim myLastRowIndex1 As Integer = DataGridView1.Rows.GetLastRow(DataGridViewElementStates.None)
Dim myLastRowIndex2 As Integer = DataGridView1.Rows.Count - 1


Hope that helps.

Cheers,

Ian
[Edit] BTW, Do not forgot to take into account the NewRow at the End of the DataGridView like I have just done.
 
Last edited:
Back
Top