DataGrid控件点击事件无数据导致获取错误标题看着有点乱……使用了DataGrid控件点击事件当点击DataGrid控件
DataGrid控件点击事件无数据导致获取错误
标题看着有点乱……
使用了DataGrid控件点击事件
当点击DataGrid控件时
读取点击行的数据
Private Sub DataGrid1_Click()
变量1 = DataGrid1.Columns("字段名1")
'...
End Sub
从直观上看起来
这个没有错误
并且正常使用时也不会发生错误
但实际上
当DataGrid框内还没有读取数据时
点击DataGrid框就会报6147
而当DataGrid中的数据太少
也就是没出现上下滚动条时
点击DataGrid框内无数据处也会报6160
曾想用On Error Goto来截取
却发现没有作用
有什么办法处理这种情况呢?
[解决办法]测试,完全没有问题
Dim rs As ADODB.Recordset
Private Sub DataGrid1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim i As Long
Dim j As Long
With DataGrid1
i = .RowContaining(Y)
j = .ColContaining(X)
If i >= 0 And j >= 0 Then
MsgBox "点击到数据区"
.Row = i
.Col = j
Else
MsgBox "点击到空白区"
End If
End With
End Sub
Private Sub Form_Load()
Dim i As Long
Dim j As Long
Dim s As String
Dim k As Long
Set rs = New ADODB.Recordset
rs.Fields.Append "Fld1", 202, 30
rs.Fields.Append "Fld2", 202, 30
rs.Fields.Append "Fld3", 202, 30
rs.Fields.Append "Fld4", 202, 30
rs.Fields.Append "Fld5", 202, 30
rs.Fields.Append "Fld6", 202, 30
rs.Fields.Append "Fld7", 202, 30
rs.Open
Randomize
For i = 0 To 10
rs.AddNew
For k = 0 To rs.Fields.Count - 1
s = ""
For j = 0 To Int(30 * Rnd) - 1
s = s & Chr(Asc("A") + (Asc("z") - Asc("A")) * Rnd)
Next
rs(k) = s
Next
Next
Set DataGrid1.DataSource = rs
End Sub