无法更新vb6中datagrid中的任何单元格

时间:2010-05-29 15:48:58

标签: binding datagrid vb6 ado

我正在尝试更新datagrid中的一行,但问题是我甚至无法更改其单元格值。 我已将datagrid AllowUpdate属性设置为true,但我仍然无法更改任何单元格值:

Option Explicit
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim recordset As New ADODB.recordset
Public Action As String
Public Person_Id As Integer
Public Selected_Person_Id As Integer
Public Phone_Type As String

Public Sub InitializeConnection()
Dim str As String

str = _
   "Provider=Microsoft.Jet.OLEDB.4.0;" & _
   "Data Source=" + App.Path + "\phonebook.mdb;" & _
   "Persist Security Info=False"

    conn.CursorLocation = adUseClient

    If conn.state = 0 Then
    conn.ConnectionString = str



    conn.Open (conn.ConnectionString)
    End If

End Sub
Public Sub AbandonConnection()
    If conn.state <> 0 Then
        conn.Close
    End If
End Sub
Public Sub Persons_Read()

    Dim qry_all As String

   ' qry_all = "select * from person,web,phone Where web.personid = person.id And phone.personid = person.id"
    qry_all = "SELECT * FROM person order by id"

    Call InitializeConnection

    cmd.CommandText = qry_all
    cmd.CommandType = adCmdText

    Set cmd.ActiveConnection = conn


    If conn.state = 1 Then

       Set recordset = cmd.Execute()

    End If

        BindDatagrid


End Sub

Private Function Person_Delete(id As Integer)
   Dim qry_all As String
    qry_all = "Delete * from person where person.id= " & id & " "

    Call InitializeConnection

    cmd.CommandText = qry_all
    cmd.CommandType = adCmdText

    Set cmd.ActiveConnection = conn


    If conn.state = 1 Then

       Set recordset = cmd.Execute()

    End If

        dg_Persons.Refresh



End Function
Private Function Person_Update()

End Function
Public Sub BindDatagrid()



   Set Me.dg_Persons.DataSource = recordset


    Me.dg_Persons.Refresh
    dg_Persons.Columns(0).Visible = False
    dg_Persons.Columns(4).Visible = False

    dg_Persons.Columns(1).Caption = "Name"


    dg_Persons.Columns(2).Caption = "Family"
    dg_Persons.Columns(3).Caption = "Nickname"
    dg_Persons.Columns(5).Caption = "Title"
    dg_Persons.Columns(6).Caption = "Job"

End Sub
Public Function DatagridReferesh()

    Call Me.Persons_Read

End Function

Private Sub cmd_Add_Click()
    frm_Person_Add.Caption = "Add a new person"
    frm_Person_Add.Show



End Sub

Private Sub cmd_Business_Click()


  '  frm_Phone.Caption = "Business Phones"
    frm_Phone.Phone_Type = "Business"
    frm_Phone.Person_Id = Selected_Person_Id

    frm_Phone.Tag = Selected_Person_Id

    frm_Phone.Show

End Sub

Private Sub cmd_Delete_Click()
  Dim msg_input As Integer

  msg_input = MsgBox("Are you sure you want to delete this person ?", vbYesNo)

    If msg_input = vbYes Then
        Person_Delete Selected_Person_Id
        MsgBox ("The person is deleted")
        frm_Phone.DatagridReferesh

    End If

End Sub

Private Sub cmd_Home_Click()

    'frm_Phone.Caption = "Home Phones"
    frm_Phone.Phone_Type = "Home"
    frm_Phone.Person_Id = Selected_Person_Id
    frm_Phone.Tag = Selected_Person_Id

    frm_Phone.Show

End Sub

Private Sub cmd_Update_Click()
   If Not Selected_Person_Id = 0 Then
     frm_Person_Edit.Person_Id = Selected_Person_Id
     frm_Person_Edit.Show
    Else
    MsgBox "No person is selected"

   End If


End Sub


Public Function AddParam(name As String, param As Variant, paramType As DataTypeEnum) As ADODB.Parameter
    If param = "" Or param = Null Then
        param = " "
    End If
    Dim objParam As New ADODB.Parameter
    Set objParam = cmd.CreateParameter(name, paramType, adParamInput, Len(param), param)
    objParam.Value = Trim(param)
    Set AddParam = objParam

End Function


Private Sub Command1_Click()
    DatagridReferesh
End Sub

Private Sub Command2_Click()
    frm_Internet.Person_Id = Selected_Person_Id
    frm_Internet.Show

End Sub

Private Sub dg_Persons_BeforeColEdit(ByVal ColIndex As Integer, ByVal KeyAscii As Integer, Cancel As Integer)


   ' MsgBox ColIndex

'    dg_Persons.Columns(ColIndex).Text = "S"
 '   dg_Persons.Columns(ColIndex).Locked = False
'    dg_Persons.Columns(ColIndex).Text = ""
 'dg_Persons.Columns(ColIndex).Value = ""

       'Person_Edit dg_Persons.Columns(0).Value, dg_Persons.Columns(1).Value, dg_Persons.Columns(2).Value,dg_Persons.Columns(3).Value,dg_Persons.Columns(4).Value, dg_Persons.Columns(5).Value

End Sub

Private Sub dg_Persons_BeforeColUpdate(ByVal ColIndex As Integer, OldValue As Variant, Cancel As Integer)
    MsgBox ColIndex
End Sub



Private Sub dg_Persons_Click()


If dg_Persons.Row <> -1 Then
    dg_Persons.SelBookmarks.Add Me.dg_Persons.RowBookmark(dg_Persons.Row)

    Selected_Person_Id = Val(dg_Persons.Columns(0).Value)
End If

End Sub


Private Sub Form_Load()

   ' dg_Persons.AllowUpdate = True
  '  dg_Persons.EditActive = True
   Call Persons_Read

   dg_Persons.AllowAddNew = True
 dg_Persons.Columns(2).Locked = False

End Sub
Private Function Person_Edit(id As Integer, name As String, family As String, nickname As String, title As String, job As String)


 InitializeConnection




cmd.CommandText = "Update  person set name=@name , family=@family , nickname=@nickname , title =@title , job=@job where id= " & id & ""
    cmd.Parameters.Append AddParam("name", name, adVarChar)
    cmd.Parameters.Append AddParam("family", family, adVarChar)
    cmd.Parameters.Append AddParam("nickname", nickname, adVarChar)
    cmd.Parameters.Append AddParam("title", title, adVarChar)
    cmd.Parameters.Append AddParam("job", job, adVarChar)


    cmd.ActiveConnection = conn
    cmd.CommandType = adCmdText


    cmd.Execute

End Function
Private Function Person_Search(q As String)
     Dim qry_all As String


    qry_all = "SELECT * FROM person where person.name like '%" & q & "%' or person.family like '%" & q & "%' or person.nickname like '%" & q & "%'"


    Call InitializeConnection

    cmd.CommandText = qry_all
    cmd.CommandType = adCmdText

    Set cmd.ActiveConnection = conn


    If conn.state = 1 Then

       Set recordset = cmd.Execute()

    End If

        BindDatagrid

End Function

Private Sub mnu_About_Click()
    frm_About.Show
End Sub

Private Sub submnu_exit_Click()
    End
End Sub


Private Sub txt_Search_Change()
    Person_Search txt_Search.Text
End Sub

4 个答案:

答案 0 :(得分:2)

我认为您可以通过添加ADODC控件作为Grid控件的源来实现此目的。这是一个很好的例子:

http://www.vbtutor.net/Lesson26.html

答案 1 :(得分:0)

您还可以使用浮动文本框控制技巧,将文本框放在网格上所需单元格的顶部,以允许用户编辑网格中的数据。

http://www.thescarms.com/vbasic/editgrid.aspx

答案 2 :(得分:0)

我意识到这个答案很古老,但万一有人需要这个,这是一个解决方案。基本上,您需要通过数据环境(以及实际指定要显示的数据的命令)获取要在数据网格内显示的数据。以下语句将更新名为DataGrid1的数据网格。

DataEnvironment1.rsCommand1.Requery
Set DataGrid1.DataSource=DataEnvironment1

其中DataEnvironment1是包含数据成员(命令)Command1的数据环境。(rsCommand1是与ResultSet对应的自动生成的Command1

所以,例如,您可以将这些语句放在Update按钮的onClick事件中,然后数据网格实时刷新!

答案 3 :(得分:0)

Dim Para3, Para4 As String
Para3 = "01-01-" & Combo1.Text
Para4 = "31-12-" & Combo1.Text
With DataEnvironment1
If .Connection2.State <> 0 Then .Connection2.Close
DoEvents
.Connection2.Open
.Command2 Para3, Para4
Set DataGrid1.DataSource = .rsCommand2
DataGrid1.ReBind
End With

我尝试在使用查询之间选择包含数据的记录。我第一次得到结果,但后来它没有刷新。现在,我可以在运行时动态地执行任何操作,每次我的网格显示相应的结果。