Getting or setting cursor position

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
In the old VB6 grids, you could set cursor position using:
grd1.Row=7
grd1.Col=4

and
numNewRow = grd1.Row

I've looked and looked in helps and other places, but the new VB.net DGV doesn't do it this way, but doesn't say how you can achieve it.

How can I do this please? It sounds a really simple basic action that should be obvious for me, but I can't work it out! Thanks again.
 
I have a DataGridView object called grd1. I have set it up as follows:
grd1.Columns.Add("0", "RecNo")
grd1.Columns.Add("1", "User Name")
grd1.Columns.Add("2", "Password")

grd1.Rows.Add(9) 'ensure one more than height, to force scroll bar.

grd1.ScrollBars = ScrollBars.Both

It has 3 columns, headed RecNo, User Name and Password.
It has 9 rows, ready for populating from the database. Good so far.
I populate it with the user names and passwords, code not included here but it works fine.

I then want to position the cursor/current cell at say, Column 2 Row 1. In the old VB6 days, I would have used:

grd1.Row = 1
grd1.Col = 2
and possibly
grd1.ShowCell(1, 2) to make sure the selected cell was visible (if the grd1 had scrolled down so row 1 wasn't in view).

What code or property of grd1 should I use to achieve the same? Thanks again.
 
Use the CurrentCell property, get a cell reference through either grid.Item(c,r) or via grid.Rows(r).Cells(c).
 
Back
Top