当前位置:编程学习 > VB >>

自动引发RowColChange事件的处理

我设置了一个添加按钮,点击按钮进行添加操作,执行完INSERT的SQL语句后,再执行一次DataGrid的数据绑定操作,但在绑定的过程中,我用断点调试发现,绑定记录集后自动会引发RowColChange事件,同时在RowColChange事件中我查找的列就找不到了。
这个是界面:

相关代码:
RowColChange事件,用户每点击一次单元格,查询相关记录字段,填充TextBox控件

Private Sub AutoList_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
    Command2.Enabled = False
    OpenConn
    Dim AutoID As Integer
    On Error Resume Next
    AutoID = AutoList.Columns("编号").CellText(AutoList.Bookmark)    '这行用了On Error处理,如果不处理会出错
    Dim Rs As New ADODB.Recordset
    Set Rs = Conn.Execute("SELECT * FROM auto_Auto WHERE AutoID = " & AutoID)
    OwnerID.BoundText = Rs("OwnerID")
    Brand.Text = Rs("Brand")
    Factory.Text = Rs("Factory")
    Model.Text = Rs("Model")
    Exit Sub
End Sub

insert/update/del操作
Private Sub Command2_Click()
    Dim Sql As String
    Sql = "INSERT INTO auto_Auto(Brand,Model,Factory,OwnerID) VALUES ('" & Brand.Text & "','" & Model.Text & "','" & Factory.Text & "'," & OwnerID.BoundText & ")"
    OpenConn
    Conn.Execute (Sql)
    BindData
    MsgBox "添加完成", vbOKOnly + vbExclamation, "完成"
End Sub

Private Sub Command3_Click()
    Dim AutoID As Integer
    AutoID = AutoList.Columns("编号").CellText(AutoList.Bookmark)
    Dim Sql As String
    Sql = "UPDATE auto_Auto SET Brand = '" & Brand.Text & "',Model = '" & Model.Text & "',Factory = '" & Factory.Text & "',OwnerID = " & OwnerID.BoundText & " WHERE AutoID = " & AutoID & ""
    OpenConn
    Conn.Execute (Sql)
    BindData
    MsgBox "编辑完成", vbOKOnly + vbExclamation, "完成"
End Sub

Private Sub Command4_Click()
    Dim AutoID As Integer
    AutoID = AutoList.Columns(0).CellText(AutoList.Bookmark)
    Dim Sql As String
    OpenConn
    Conn.Execute ("DELETE FROM auto_Auto WHERE AutoID = " & AutoID)
    BindData
    MsgBox "删除完成", vbOKOnly + vbExclamation, "完成"
End Sub

DataGrid绑定
Private Sub BindData()
    '绑定DataGrid
    Dim Rs1 As New ADODB.Recordset
    Sql = "SELECT     dbo.auto_Auto.AutoID, dbo.auto_Owner.OwnerName, dbo.auto_Auto.Brand, dbo.auto_Auto.Factory, dbo.auto_Auto.Model FROM   dbo.auto_Auto INNER JOIN                      dbo.auto_Owner ON dbo.auto_Auto.OwnerID = dbo.auto_Owner.OwnerID"
    Rs1.Open Sql, Conn, 1, 1
    Set AutoList.DataSource = Rs1
    AutoList.Columns(0).Width = 121
    AutoList.Columns(0).Caption = "编号"
    AutoList.Columns(1).Width = 120
    AutoList.Columns(1).Caption = "车主姓名"
    AutoList.Columns(2).Width = 139
    AutoList.Columns(2).Caption = "品牌"
    AutoList.Columns(3).Width = 132
    AutoList.Columns(3).Caption = "型号"
    AutoList.Columns(4).Width = 195
    AutoList.Columns(4).Caption = "生产厂家"
End Sub


主要代码和界面都在上面了,我就是想问一下,当添加或编辑时,在RowColChange事件中只能用On error处理么,有没有别的办法呢? --------------------编程问答-------------------- 你在另一个帖,不是已经告诉你要怎么做了吗? --------------------编程问答-------------------- 说明一下吧,还是没搞明白 --------------------编程问答-------------------- --------------------编程问答-------------------- 我是要在点击Datagrid上的单元格的时候,把这个记录的各字段值放到控件中让用户修改,修改完成再点击编辑按钮完成编辑,可能我的思路不对吧 --------------------编程问答--------------------
引用 4 楼 xiaojie_cp 的回复:
我是要在点击Datagrid上的单元格的时候,把这个记录的各字段值放到控件中让用户修改,修改完成再点击编辑按钮完成编辑,可能我的思路不对吧


在另外一个贴,不是告诉你该怎么弄了? --------------------编程问答--------------------

Private Sub BindData()
    '绑定DataGrid
    Dim Rs1 As New ADODB.Recordset
    Private Sub BindData()
    '绑定DataGrid
    Dim Rs1 As New ADODB.Recordset
    Sql = "SELECT A.AutoID As 编号, B.OwnerName As 车主姓名, A.Brand As 品牌, A.Factory As 生产厂家, A.Model As 型号 FROM dbo.auto_Auto As A INNER JOIN dbo.auto_Owner As B ON A.OwnerID = B.OwnerID"
    Rs1.Open Sql, Conn, 1, 1
    Set AutoList.DataSource = Rs1
End Sub

Private Sub Form_Load()
    AutoList.Columns(0).Width = 121
    AutoList.Columns(1).Width = 120
    AutoList.Columns(2).Width = 139
    AutoList.Columns(3).Width = 132
    AutoList.Columns(4).Width = 195
End Sub
补充:VB ,  控件
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,