如何实现在DataGrid或者其他Grid控件中编辑\增删改记录信息

解决方案 »

  1.   

    編輯可以自動完成的Option ExplicitPublic rs As New ADODB.Recordset
    Public conn As New ADODB.ConnectionPrivate Sub cmdAdd_Click()
     On Error Resume Next
     rs.AddNew 
    End SubPrivate Sub cmdDel_Click()
     On Error Resume Next
     rs.Delete
     
    End SubPrivate Sub DataGrid1_ButtonClick(ByVal ColIndex As Integer)
     List1.AddItem "Female"
     List1.AddItem "Male"
     List1.Visible = TrueEnd Sub
    Private Sub Form_Load()
     Dim strconn As String
     strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\test.mdb;Persist Security Info=False"
     conn.CursorLocation = adUseClient
     
     conn.Open strconn
     
     If rs.State = 1 Then rs.Close
     rs.Open "Select * from People", conn, adOpenKeyset, adLockPessimistic
     
     Set Me.DataGrid1.DataSource = rs
     
     With Me.DataGrid1
       .Columns(0).Caption = "ID"
       .Columns(1).Caption = "Name"
       .Columns(2).Caption = "Sex"
       .Columns(3).Caption = "Meno"
       .Columns(0).Width = 0
       .Columns(1).Width = 1200
       .Columns(2).Width = 1200
       .Columns(3).Width = 1500
      
       .Columns(2).Button = True
       
     End With
    End SubPrivate Sub Form_Unload(Cancel As Integer)
     rs.MoveFirst
     
     rs.Close
     Set conn = Nothing
    End SubPrivate Sub List1_Click()
     Me.DataGrid1.Columns(2).Text = Me.List1.Text
     Me.List1.Visible = False
     
    End Sub
      

  2.   

    MSHFLEXGRID的输入
    '***************************Private Sub Form_Load()
            Text1.Visible = False
            MSHFlexGrid1.RowHeight(-1) = 270
    End SubPrivate Sub MSHFlexGrid1_Click()
               With MSHFlexGrid1
                    Text1.Visible = False
                    Text1.Move .Left + .CellLeft, .Top + .CellTop, .CellWidth, .CellHeight
                    Text1.Text = .Text
                    Text1.Visible = True
                    Text1.SetFocus
               End With
    End SubPrivate Sub Text1_Change()
            MSHFlexGrid1.Text = Text1.Text
    End Sub
      

  3.   

    '
    '删除指定范围的行数
    '函数:KillGridRows
    '参数:StarRow 删除的开始行 ,EndRow 删除的结束行.
    '返回值:
    Public Sub KillGridRows(StarRow As Long, EndRow As Long)
        Dim RowID As Long, AddID As Long, A As Long, B As Long
        Dim RemoArr() As Long
        
        Timer1.Enabled = False
        UserControl.Extender.Visible = False
        Ev_GridObj.Redraw = False
        For RowID = StarRow To EndRow
            AddID = AddID + 1
            ReDim Preserve RemoArr(AddID)
            RemoArr(AddID - 1) = RowID
        Next
        
        If ArrEmpty(RemoArr) Then
           For RowID = 0 To UBound(RemoArr) - 1
               Ev_GridObj.RemoveItem RemoArr(RowID)
               DoEvents
               If RowID <= UBound(RemoArr) - 1 Then
                  For A = RowID To UBound(RemoArr) - 1
                      RemoArr(A) = RemoArr(A) - 1
                  Next
               End If
           Next RowID
        End If
        Ev_GridObj.Redraw = True
        M_OleRow = 0: M_OleCol = 0
        Timer1.Enabled = M_Author
    End Sub
      

  4.   

    '
    '删除指定范围的列数
    '函数:KillGridCols
    '参数:StarCol 删除的开始列 ,EndCol 删除的结束列.
    '返回值:
    Public Sub KillGridCols(StarCol As Long, EndCol As Long)
        Dim MoveStr As String
        Dim RowID As Long, RedCol As Long
        
        Ev_GridObj.Redraw = False
        
        If M_EditCol = Ev_GridObj.Cols - 1 Then
           Ev_GridObj.Cols = Ev_GridObj.Cols - 1
        Else
            For RowID = StarCol To Ev_GridObj.Cols - 1
                RedCol = RowID + EndCol - StarCol + 1
                If RedCol > Ev_GridObj.Cols - 1 Then Exit For
                Ev_GridObj.Row = 0: Ev_GridObj.Col = RedCol
                Ev_GridObj.RowSel = Ev_GridObj.Rows - 1: Ev_GridObj.ColSel = RedCol
                MoveStr = Ev_GridObj.Clip
                Ev_GridObj.Row = 0: Ev_GridObj.Col = RowID
                Ev_GridObj.RowSel = Ev_GridObj.Rows - 1: Ev_GridObj.ColSel = RowID
                Ev_GridObj.Clip = MoveStr
                P_GridArr(RowID) = P_GridArr(RedCol)
            Next
            If Ev_GridObj.Cols > EndCol - StarCol + 2 Then Ev_GridObj.Cols = Ev_GridObj.Cols - (EndCol - StarCol + 1)
        End If
        If M_EditCol > Ev_GridObj.Cols - 1 Then M_EditCol = Ev_GridObj.Cols - 1
        Ev_GridObj.Redraw = True
        M_OleRow = 0: M_OleCol = 0
    End Sub
      

  5.   

    '
    '删除指定范围的内容
    '函数:KillSelGrid
    '参数:StarRow  删除的开始行,StarCol 删除的开始列 ,EndRow 删除的结束行 ,EndCol 删除和结束列.
    '返回值:
    Public Sub KillSelGrid(StarRow As Long, StarCol As Long, EndRow As Long, EndCol As Long)
        Dim RowID As Long, B As Long
        
        Timer1.Enabled = False
        UserControl.Extender.Visible = False
        For RowID = StarRow To EndRow
            For B = StarCol To EndCol
                Ev_GridObj.TextMatrix(RowID, B) = ""
            Next
        Next
        Ev_GridObj.Redraw = True
        Timer1.Enabled = M_Author
    End Sub
      

  6.   

    '
    '表格当前行前插入空行
    '函数:InsertRow
    '参数:InsRows 插入的空行数.
    '返回值:
    Public Sub InsertRow(Optional InsRows As Long = 1)
        Dim Rs As Long, Rend As Long
        Dim Cs As Long, Cend As Long
        Dim MoveStr As String
        
        On Error Resume Next
        
        If InsRows <= 0 Then Exit Sub
        Timer1.Enabled = False
        With Ev_GridObj
             UserControl.Extender.Visible = False
             .Redraw = False
            .Rows = .Rows + InsRows
                   
            For Rs = .Rows - InsRows - 1 To M_EditRow Step -1
                .Row = Rs: .Col = 0
                .RowSel = Rs: .ColSel = .Cols - 1
                MoveStr = .Clip
                .Row = Rs + InsRows: .Col = 0
                .RowSel = Rs + InsRows: .ColSel = .Cols - 1
                .Clip = MoveStr
            Next Rs
            
            For Rs = M_EditRow To M_EditRow + InsRows - 1
                For Cs = 0 To .Cols - 1
                    .TextMatrix(Rs, Cs) = ""
                Next Cs
            Next Rs
            .Redraw = True
        End With
        Timer1.Enabled = M_Author
    End Sub