VB 如何读取DataGrid中指定的单元格的数据

2024-12-03 12:04:03
推荐回答(1个)
回答1:

给你一个例子

Dim Conn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Rs.Open "SELECT * FROM 报警记录 where " & Te.Tag & "='" & Text1.Text & "'", Conn
If Rs.RecordCount = 0 Then
MsgBox vbCrLf & "“" & Rs.Fields(0) & "”查找失败!", vbInformation, " 提 示"
Rs.Close
Rs.Open "select * from 报警记录", Conn, adOpenStatic, adLockOptimistic
End If
Set DataGrid1.DataSource = Rs
For Each Col In DataGrid1.Columns '修改列表
If Col.DataField = "序号" Or Col.DataField = "备注" Then Col.Visible = False '隐藏不想显示的字段
Next Col
DataGrid1.Columns(11).Caption = "电压"‘替换显示将数据库里的字段
if Rs.Fields(4)="10" then msgbox "数值过低!"'第5(从0开始)个单元格数值
For I = 0 To 14
DataGrid1.Columns(I).Caption = Space(((DataGrid1.Columns(I).Width - Len(DataGrid1.Columns(I).Caption) * 150) \ 2) \ 150) & DataGrid1.Columns(I).Caption '标题居中
DataGrid1.Columns(I).Alignment = dbgCenter
Next
DataGrid1.Refresh
Me.Caption = "工作记录[信息总数:" & Rs.RecordCount & "]"’最大行数