Display Datagrid with checkboxes

db , vb6 Ghana
  • 12 years ago

    Please I've been struggling to use a datagrid control to display records with check Boxes which I could not, and has been come a headache for me.

    Can any kind person/Guru help me solve this problem. For God's sake. I've been going from site to site, but helplessly unattended to. 

    Please help me solve it.

     

    Thank you, and hope 2 hear from you soon

  • 12 years ago

    Hey,

     

    I nicked this from http://www.codeguru.com/vb/gen/vb_database/datagrid/article.php/c7587/

    There is example code there also.

     

    Quick example
    Need a checkbox called cb ; index = 0 ; width & height = 190
    datagrid = Grid


    Private Sub Form_Load()
      'My fourth column is the check box column
      SetCheckboxes 3, cb
    End Sub

    Private Sub SetCheckboxes(ColNdx As Long, ByRef ChkboxArray As Object)
        bInSetCheckboxes = True
    On Error GoTo ErrorExit
        Dim i
        Dim obj As Object
        Set obj = Grid
           
        Dim OffsetX As Long, OffsetY As Long
        If Not ChkboxArray(0).Container Is Grid.Container Then
            CalcContainerOffset obj, OffsetX, OffsetY
        End If
       
        On Error Resume Next
       
        With Grid
            If (ChkboxArray.UBound <> .VisibleRows) Then
                For i = ChkboxArray.UBound + 1 To .VisibleRows - 1
                    Load ChkboxArray(i)
                    ChkboxArray(i).Width = 190
                    ChkboxArray(i).Height = 190
                Next
                For i = .VisibleRows To ChkboxArray.UBound
                    Unload ChkboxArray(i)
                Next
            End If
       
            OffsetX = OffsetX + (.Columns(ColNdx).Width - ChkboxArray(0).Width) / 2
            OffsetY = OffsetY + 10 ''(.RowHeight - ChkboxArray(0).Height) / 2

            .Columns(ColNdx).Alignment = dbgCenter
            .Columns(ColNdx).Locked = True
           
            '
            If .LeftCol <= ColNdx Then
                For i = 0 To .VisibleRows - 1
                    ChkboxArray(i).Value = Abs(.Columns(ColNdx).CellValue(.RowBookmark(i)))
                    ChkboxArray(i).Top = .Top + .RowTop(i) + OffsetY
                    ChkboxArray(i).Left = .Left + .Columns(ColNdx).Left + OffsetX
                    ChkboxArray(i).Visible = True
                    ChkboxArray(i).ZOrder
                Next
            Else
                i = 0
            End If
           
            For i = i To ChkboxArray.UBound
                ChkboxArray(i).Visible = False
            Next
           
        End With
      
    ExitPoint:
        bInSetCheckboxes = False
        Exit Sub

    ErrorExit:
        Resume ExitPoint
    End Sub

     


    Public Function CalcContainerOffset( _
            obj As Object, _
            ByRef OffsetX As Long, _
            ByRef OffsetY As Long _
        )
       
       
        Do While Not (obj.Container Is obj.Parent)
            Set obj = obj.Container
            If Not (obj Is Nothing) Then
                OffsetX = OffsetX + obj.Left
                OffsetY = OffsetY + obj.Top
               
                '' The offsets for borders below are not exact for frames,
                '' this positioning algorithm works perfectly at any depth
                '' of nested pictureboxes, with any combination of borders
                '' and/or 3D at any levels.
                ''
                '' Using a frame with borders and/or 3d throws a visible skew
                '' on the positions but this should be fixable with some trial
                '' and error -- the skew is no more than 2 px. per frame.
                ''
                '' Other containers may be subject to different metrics.
                ''
                If obj.BorderStyle = 1 Then '' fixed single
                    If obj.Appearance = 1 Then '' 3d
                        OffsetX = OffsetX + 30
                        OffsetY = OffsetY + 30
                    Else
                        OffsetX = OffsetX + 15
                        OffsetY = OffsetY + 15
                    End If
                End If
            End If
            If (TypeOf obj Is Form) Or (TypeOf obj Is MDIForm) Then Exit Do
        Loop


    End Function


     

     

     

     

  • 12 years ago
    Hey Guru, Sorry for the delay in reply. Thanks a lot for attending to me. It works fine! may the good Lord bless u. Ben

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Linux is only free if your time has no value” - Jamie Zawinski