DataGridView - make checkbox in row optional

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
Hi all.

VB 2005, DataGridView. I have succeeded in giving all the cells in one column a checkbox - I can check them and uncheck them, no problem.

However, I would like some rows not to have a checkbox in that column (where the logic of having one there is wrong).

I have 2 procedures for dgvs - a SetupDgv procedure which displays a correctly headed dgv with blank rows, then a FillDgv procedure which just populates the rows. Currently the SetupDgv gives each cell in the column a tickbox - even the bottom few rows which are empty anyway.

The code I am using to show the checkboxes in SetupDgv is
VB.NET:
Dim chk As New DataGridViewCheckBoxColumn
        With chk
            .Name = "4"
            .Width = 50
            .HeaderText = "Selected"
            .DefaultCellStyle.NullValue = CheckState.Unchecked
            .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        End With
        grd1.Columns.Add(chk)

where grd1 is the name of the dgv.

I would like to know how to 'turn off' a checkbox in the FillDgv procedure, as in
VB.NET:
                    If mblnPathFileAndTagFromRootExists = True Then
                        strYN = "Y"
                        strFullLocalPath = mstrPathFileAndTagFromRoot

                        'Make col 4 have a checkbox. Done.

                    Else
                        strYN = ""

                        'Make col 4 NOT have a checkbox. Hmmmm.
                        '?????????

                    End If

whilst populating each row. Of course, each FillDgv session may have different data and therefore checkboxes on different lines.

Thank you for any suggestions you can make.
 
I approach it slightly differently by utilising the CellPainting event. The following assumes that the logic for the checkbox can be obtained from within the DGV itself.

VB.NET:
        Select Case grd1.Columns(e.ColumnIndex).Name.ToUpper.Trim
            Case "CHECKBOX_COLUMN_NAME"
                If Convert.ToInt32(grd1.Rows(e.RowIndex).Cells("VALUE_TO_BE_CHECKED_COLUMN").Value) = 0 Then
                    'checkbox not required
                    If grd1.Rows(e.RowIndex).Selected = True Then
                        'highlighted
                        e.Graphics.FillRectangle(brHighlightedCell, e.CellBounds)
                    Else
                        e.Graphics.FillRectangle(brCompletedCell, e.CellBounds)
                        'not highlighted
                    End If
                    e.Graphics.DrawLine(pGridLine, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1)
                    e.Graphics.DrawLine(pGridLine, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)
                    e.Handled = True
                    Exit Sub
                End If
        End Select
        e.Handled = False

You may have to play around with the colours you require based on whether the cell / row is selected.
 
This is great, thank you. I made it slightly more difficult for myself by using colour names as strings, and using
VB.NET:
Me.BackColor = System.Drawing.Color.FromName(mstrScreenColour1)
for colours.

In 'e.Graphics.FillRectangle(Brushes.White, e.CellBounds)' the Brushes collection does not seem to have a .FromName method. Ah well. White will be fine!

Thanks again, I appreciate your help.
 
the Brushes collection does not seem to have a .FromName method.
You can create a new SolidBrush from a given Color.

It is also a possibility to make the column a checkbox column, but use the CellPainting to only paint the background without checkbox in those cases. e.PaintBackground can be used, and you can configure colors for column in DefaultCellStyle in designer.
 
Back
Top