Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checkbox on a datagrid column 1

Status
Not open for further replies.

zyrag

IS-IT--Management
Dec 4, 2002
252
PH
i have a query which extract transaction nos. not yet posted. The result will be displayed into a datagrid. On the datagrid, i want to have a checkbox column where the user could check or uncheck to signify that the record be posted or not. Within the form, i also have 2 option buttons, 1st button labeled "All" and the 2nd labeled "Selected". If the user clicks on option "All" then the checkboxes on the datagrid will all be checked.

Is this possible? How do i achieve this?

thanks,
 
as i scan through this forum and on google.com, it seems that this feature could not be done in VB6 using the datagrid control. Has anyone successfully done this feature using a third party grid?
 
>checkbox column
Niether the DataGrid, nor the FlexGrid offer a simple way, if at all, to add a checkbox column.
The ListView works good for this but this means learning how to add the data and use it, which is totally different from using a bound control like a DataGrid.

Because the DataGrid is bound, you would need to add a new field to the db table which it is bound to (Integer or Boolean, or even text).
Then you can use the column's button property to add a button arrow to the cell (only visible for the cell that the cursor is currently on).
Then, in the button click event you can toggle between two values (True/False, Yes/No, or even two different text strings).
Then the user sees something like "Checked" or "True" in that column for the items considered to be in this state.

In a flex grid, you could add an extra column and then set the CellPicture to a picture of a checked or unchecked picture box and toggle between the two pictures when the "cell" is clicked.
 
Another option with the flex grid is to use a floating checkbox control and manually set it depending on the value of the underlying cell. You would need a control array (one for each row) and be able to place the controls so that they seem to be a part of the grid. This can become tedious when dealing with scroll events.

See faq222-3262 for explanaition and code for floating controls. You ca also search this and forum222.

Thanks and Good Luck!

zemp
 

Another good point zemp which I forgot!
This is just as possible to do with the DataGrid zyrag.
Because of the scrolling problems, it may work best with a combination of using the CellPicture for all cells which I mentioned, and the floating control which zemp mentions for just the active cell...
 

The first column (column 0) is used for this check box.
Create two check box bitmaps, one for a Checked state, and one for UnChecked (just add one to a vb form, change the Appearance property to 2-D, do a screen print, pasted it into MS Paint, cut out the check box portion, open a new Paint job and paste, set the picture size to 16 x 16, etc., then save it. Then for the UnChecked box just fill the Check with white color and save it under another name).
Then, add a Check box some where on the same container as the FlexGrid and set it's Visible property to False, Appearance to 2-D, Backcolor to grey, no caption, width and height to 240, and the Alignment to Right.

Then, just to test it:

Code:
Public Sub Display(rs As ADODB.Recordset)
    With MSHFlexGrid1
        Set .DataSource = rs
        .Redraw = False
        .Col = 0
        .FixedCols = 0
        Dim i As Integer
        For i = 1 To .Rows - 1
            .Row = i
            If i Mod 2 = 0 Then
                Set .CellPicture = LoadPicture("H:\Graphics\ChkOn.bmp")
                .RowData(i) = 1
            Else
                Set .CellPicture = LoadPicture("H:\Graphics\ChkOff.bmp")
                .RowData(i) = 0
            End If
        Next i
        .ColAlignment(0) = flexAlignCenterCenter
        .ColWidth(0) = 240
        .Redraw = True
    End With
    Me.Show
    MSHFlexGrid1.SetFocus
End Sub

Private Sub Check1_Click()
    With MSHFlexGrid1
        If Check1.Value = vbChecked Then
            Set .CellPicture = LoadPicture("H:\Graphics\ChkOn.bmp")
            .RowData(i) = 1
        Else
            Set .CellPicture = LoadPicture("H:\Graphics\ChkOff.bmp")
            .RowData(i) = 0
        End If
    End With
End Sub

Private Sub MSHFlexGrid1_RowColChange()
    With MSHFlexGrid1
        If .Col = 0 Then
            Check1.Left = MSHFlexGrid1.Left
            Check1.Top = MSHFlexGrid1.CellTop + MSHFlexGrid1.Top
            .RowData(.Row) = Abs(Not (CBool(.RowData(.Row))))
            Check1.Value = .RowData(.Row)
            Check1_Click
            Check1.Visible = True
        Else
            Check1.Visible = False
        End If
    End With
End Sub

Load the form by calling:
Form2.Display
This assumes that you are passing an already opened recordset. If not, then remove the argument and open the recordset in the Display proceedure, or add a Adodc to the form, set it's ConnectionString and RecordSource properties, and bind the Grid's datasource to it.

Of course, you may need to fine tune this....
 
thanks guys, to make it simple, i used CCLINT's suggestion of making the column layout as a button and toggle that value at the datagird_ButtonClick event. Though may not look as elegant as it should be, but it serves the purpose.
 

Thanks zyrag.


Concerning the FlexGrid:
A couple of errors in the FlexGrid code:

1. use .FixedCols = 1 prior to setting the DataSource and FixedCols = 0 right afterwards.

2. In the CheckBox1_Click event change
.RowData(i) = 1
and
.RowData(i) = 0

to
.RowData(.Row) = 1
and
.RowData(.Row) = 0


Anyone see other serious problems with this?

You should have checkboxes (actually images) for each row (the actual state and .RowData value will need to be set in the row loop of the Display proceedure) which look exactly like a VB 2-D Checkbox (could also use a 3-D box) and when clicked on it's state changes - just as if every row had it's own real checkbox, with no differences.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top