locked
checkbox column in datagridview RRS feed

  • Question

  • Hi !! I have been working on this problem for 2 days, i am coding in visual basic, with visual studio 2005,  I already have the checkbox column in my datagridview but it doesn`t validate anything in the datagridview.  What I need is that when i check a particular box it will "cancel" the row,  (gray it), umm and paint the row in another color. I need to change the row color immediatly when the checkbox is checked. and after a row is checked and colored it has to be saved so that every time i start the application again, I can see the changes made. this is what i have til now...please help...

     Friend SelectedRowColor As Color = Color.LightGray
        Friend NormalRowColor As Color

        Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

            If e.ColumnIndex = Me.DataGridView1.Columns.Item("checkboxcancelcolumn").Index Then
                Dim chkCell As DataGridViewCheckBoxCell = Me.DataGridView1.Rows(e.RowIndex).Cells("checkboxcancelcolumn")
                chkCell.Value = Not chkCell.Value

            End If
            MsgBox("STATE: " & Me.DataGridView1.Rows(e.RowIndex).Cells("checkboxcancelcolumn").Value)

        End Sub
        Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
            'Change the backcolor only when the checkbox column's value has changed.
            If DataGridView1.Columns(e.ColumnIndex).Name = "checkboxcancelcolumn" Then
                'Choose the color based on the checkbox cell value.
                Dim clr As Color = SelectedRowColor
                If DataGridView1.Rows(e.RowIndex).Cells("checkboxcancelcolumn").Value = False Then
                    clr = NormalRowColor
                End If
                For Each cell As DataGridViewCell In DataGridView1.Rows(e.RowIndex).Cells
                    cell.Style.BackColor = Color.LightCyan
                Next
            End If
        End Sub
    Thursday, August 28, 2008 4:42 PM

Answers

  • Hi LizYE

     

    Well, we need to make some trick while using CheckBoxColumn. Please refer to the following code for implementing this requirement (please pay attention on CommitEdit, which is used to commit changed checkbox value immediately, otherwise, it will only take effect when we select another row):

    Code Snippet

    Private Sub dataGridView1_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)

        ' See whether the current cell is the checkbox column.

        If e.ColumnIndex = 4 Then

            ' Commit the changed value of checkbox, it's the key to implement this function.

            If dataGridView1.IsCurrentCellDirty Then

                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)

            End If

           

            If dataGridView1.CurrentCell.Value.ToString().Equals("True") Then

                dataGridView1.CurrentRow.[ReadOnly] = True

                For Each dgvCell As DataGridViewCell In dataGridView1.CurrentRow.Cells

                    dgvCell.Style.BackColor = Color.LightGray

                Next

               

                ' Update the current data row.

                Me.table2BindingSource.EndEdit()

                Me.table2TableAdapter.Update(Me.dB1DataSet.Table2(dataGridView1.CurrentRow.Index))

               

            End If

        End If

    End Sub

     

    For more information about how to use CheckBoxColumn while value changed, please refer to the following thread:

    DataGridView Problem

     

    Please feel free to let me know if there is any question.

    Thanks.

     

    Best wishes

    Jun Wang

     

    Tuesday, September 2, 2008 9:33 AM