datagrid的问题,请高手帮帮忙
在我的程序中要对datagrid进行编辑,如图中,第一列和第四列是在新加一行时,写入到SQL表中,第三列需要按快捷码查询,结果放到listview中再进行选择,然后写到将名称写到第三列中。运行过程中,有一个“新建”按钮,第一行是在新建中加的序号和录入者,没有问题,当在第二行输入快捷码后,回车,则类似死循环一样的问题,看一下设备管理器,已经为“无响应”。
“新建”按钮代码
CR = "INSERT INTO HT_khxxls(KHXX003,CREATOR) select '0001','" & LoadPeople & "'"
ADOCN.Execute CR
Adodc2.RecordSource = "SELECT * FROM HT_khxxls WHERE CREATOR='" & LoadPeople & "' order by KHXX003"
Set DataGrid2.DataSource = Adodc2
Adodc2.Refresh
DataGrid2.Refresh
DataGrid2.Enabled = True
DataGrid2.AllowAddNew = True
DataGrid2.AllowDelete = True
DataGrid2.AllowUpdate = True
datagrid中的代码
事件AfterColEdit
Private Sub DataGrid2_AfterColEdit(ByVal ColIndex As Integer)
If DataGrid2.Col = 2 Then 'Exit Sub '第三列(信息内容)结束向三列写入发票名头、发货到站信息
Dim ConStr As String
Dim adodc As New ADODB.Connection
Dim rs1 As ADODB.Recordset
Dim rs2 As ADODB.Recordset
Set rs1 = New ADODB.Recordset
Set rs2 = New ADODB.Recordset
Dim Item As ListItem
'如果列三列为空,刚不显示list列表
If DataGrid2.Columns(2).Text = "" Then
List2.ListItems.Clear
List2.Visible = False
Exit Sub
End If
'按快捷码到表中模糊查询
sql1 = "SELECT MA001,MA003,MA102 FROM COPMA WHERE MA102 like '" & Trim(DataGrid2.Columns(2).Text) & "%' or MA001 like '" & Trim(DataGrid2.Columns(2).Text) & "%'" '根据快捷码查询
'单步运行时在下面这一句出现问题,可以查询分析器中运行没有问题呢!!!
rs1.Open sql1, ADOCN, adOpenKeyset, adLockOptimistic
If rs1.RecordCount = 1 Then '快捷码查询结果唯一时,直接将结果写入datagrid中
rs1.MoveFirst
DataGrid2.Columns(2).Text = rs1("MA003")
List2.Visible = False
ElseIf rs1.RecordCount > 1 Then '快捷码查到多个信息,将结果写到list列表中供选择
rs1.MoveFirst
List2.Sorted = False
List2.ListItems.Clear
Do While Not rs1.EOF 'And i < rs1.PageSize
Set Item = List2.ListItems.Add(, , , , 1)
With Item
.SubItems(1) = IIf(IsNull(rs1("MA001")), "", Trim(rs1("MA001")))
.SubItems(2) = IIf(IsNull(rs1("MA003")), "", Trim(rs1("MA003")))
.SubItems(3) = IIf(IsNull(rs1("MA102")), "", Trim(rs1("MA102")))
End With
rs1.MoveNext
Loop
’指定list显示的位置并令其可见
List2.Left = 9960
List2.Top = 3250 + DataGrid2.RowTop(DataGrid2.Row) + DataGrid2.RowHeight
List2.Visible = True
End If
rs1.Close
End If
End Sub
如果将上面事件都注释掉则在下面的事件中出现问题
Private Sub DataGrid2_AfterInsert()
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
sql = "SELECT max(KHXX003) AS MAXXH2 FROM HT_khxxls WHERE CREATOR='" & LoadPeople & "'"
rs.Open sql, ADOCN, adOpenKeyset, adLockOptimistic
If rs.RecordCount > 0 Then
XH = rs("MAXXH2") + 1
Else
XH = "0001"
End If
下面被注释的这几行,在正常按顺序录入时没有问题,一但记录数超过8行(8是当前控件显示的行数),将滚动条往回拉,或在修改时,再增加记录时,则提示DataGrid2.Row = 8这一行出错“行号无效"
' If Adodc2.Recordset.RecordCount > 1 And Adodc2.Recordset.RecordCount < 10 Then
' DataGrid2.Row = Adodc2.Recordset.RecordCount - 1
' Else
' DataGrid2.Row = 8
' End If
' DataGrid2.Columns(3).Text = LoadPeople
' DataGrid2.Columns(0).Text = Format(XH, "0000") 'DataGrid2.Count
' DataGrid2.Refresh
由于上面注释部分有问题,又想的下面这个办法,先在SQL中插入记录,再刷新datagrid,结果出现死机的状态,按步跟踪也踪不到
CR = "INSERT INTO HT_khxxls(KHXX003,CREATOR) select '" & Format(XH, "0000") & "','" & LoadPeople & "'"
ADOCN.Execute CR
Adodc2.RecordSource = "SELECT * FROM HT_khxxls WHERE CREATOR='" & LoadPeople & "' order by KHXX003"
Set DataGrid2.DataSource = Adodc2
Adodc2.Refresh
DataGrid2.Refresh
End Sub
下面是datagrid的其它事件,但和第三列都没什么关系
Private Sub DataGrid2_BeforeColUpdate(ByVal ColIndex As Integer, OldValue As Variant, Cancel As Integer)
If xChangeItem = "xg" Then
If DataGrid2.Col = 2 Then
Label11 = OldValue
End If
End If
End Sub
Private Sub DataGrid2_BeforeDelete(Cancel As Integer)
Dim mResult As Integer
If Cancel = False Then str = DataGrid2.Columns(0).Text
mResult = MsgBox("确定删除该记录吗?", vbQuestion + vbYesNo, "提示信息")
If mResult = vbNo Then
Cancel = True
End If
End Sub
Private Sub DataGrid2_ButtonClick(ByVal ColIndex As Integer)
If Not List.Visible Then
List.Visible = True
List.Width = DataGrid2.Columns(1).Width
' List.Height = 780
List.Left = DataGrid2.Left + DataGrid2.Columns(1).Left
List.Top = DataGrid2.Top + DataGrid2.RowTop(DataGrid2.Row) + DataGrid2.RowHeight
'.RowTop返回一个包含datagrid控件的一个指定行顶部的纵坐标。
End If
End Sub
Private Sub DataGrid2_Click()
List1.Visible = False
List2.Visible = False
List3.Visible = False
MonthView1.Visible = False
End Sub
Private Sub DataGrid2_Error(ByVal DataError As Integer, Response As Integer)
Response = 0
End Sub
Private Sub DataGrid2_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
List.Visible = False
End Sub
[解决办法]
呵呵,有时候就那么简单,头脑一醒问题也就解决了。
[解决办法]
datagrid,n年没用了。