DataGrid 是面向记录集的。也就是说,它的所有操作都是基于记录集的。你可以这样做: 1 用 rs.AbsolutePosition = n 或 rs.Move n 等方式定位到一条记录,同时也就定位到 Datagrid 的某一行。2 用 sendkeys "{Tab}" 移动到某一列。
参照: Private Sub Command1_Click() DataGrid1.SetFocus '让第3行第2列获得焦点 DataGrid1.Row = 2 DataGrid1.Col = 1 '打印第3行第2列的值 Debug.Print DataGrid1.Text End Sub
写成函数,方便多次调用Private Sub Setfocus(ByVal row As Long, ByVal col As Integer) With DataGrid1 .Setfocus .row = row .col = col End With End SubPrivate Sub Command1_Click() Setfocus 2, 1 '调用Setfocus,让第3行第2列获得焦点 Debug.Print DataGrid1.Text '打印获得焦点的单元格内容 End Sub
不好意思,顺序写反了,这样写就对了: Private Sub Setfocu(ByVal row As Long, ByVal col As Integer) With DataGrid1 .row = row .col = col .Setfocus End With End Sub
With DataGrid1 .row = row .col = col .SetFocus .Text = "10" text1.Text = .Text End With
1 用 rs.AbsolutePosition = n 或 rs.Move n 等方式定位到一条记录,同时也就定位到 Datagrid 的某一行。2 用 sendkeys "{Tab}" 移动到某一列。
Private Sub Command1_Click()
DataGrid1.SetFocus
'让第3行第2列获得焦点
DataGrid1.Row = 2
DataGrid1.Col = 1
'打印第3行第2列的值
Debug.Print DataGrid1.Text
End Sub
With DataGrid1
.Setfocus
.row = row
.col = col
End With
End SubPrivate Sub Command1_Click()
Setfocus 2, 1 '调用Setfocus,让第3行第2列获得焦点
Debug.Print DataGrid1.Text '打印获得焦点的单元格内容
End Sub
Private Sub Setfocu(ByVal row As Long, ByVal col As Integer)
With DataGrid1
.row = row
.col = col
.Setfocus
End With
End Sub
With DataGrid1
.row = row
.col = col
.SetFocus
.Text = "10"
text1.Text = .Text
End With