Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Update CheckBox Column based on another underlying column with an expression
 

Update CheckBox Column based on another underlying column with an expression

The code which follows displays three columns, CheckBox, Text, Text. The last column is displayed for testing to mimic changes in real time.
My question is, how can I get the checkbox column when clicked to update based on code in the CellContentClick event (is this the proper event)of the DataGridView w/o leaving the current row?

    Private Sub frmTinkering_Load() Handles MyBase.Load
        LoadDataAndConfigureGridView()
    End Sub

    Private Sub LoadDataAndConfigureGridView()
        DataGridView1.AutoGenerateColumns = False
        DataGridView1.AllowUserToAddRows = False

        Dim ColumnExpression As String = _
        "IIF(UnderlyingData = 'A','True','False')"

        Dim Table As New DataTable
        With Table.Columns
            .AddRange(New DataColumn() _
               { _
                  New DataColumn("UnderlyingData", _
                                 System.Type.GetType("System.String")), _
                  New DataColumn("FruitData", _
                                 System.Type.GetType("System.String")), _
                  New DataColumn("CheckBoxData", _
                                 System.Type.GetType("System.String"), _
                                 ColumnExpression) _
               } _
            )
        End With

        Table.Rows.Add(New Object() {"A", "Apples"})
        Table.Rows.Add(New Object() {"B", "Grapes"})
        Table.Rows.Add(New Object() {Nothing, "Pears"})

        Dim CheckList As New DataGridViewCheckBoxColumn
        With CheckList
            .HeaderText = "A/B"
            .DataPropertyName = "CheckBoxData"
            .Name = "CheckBoxData"
        End With
        Dim Fruit As New DataGridViewTextBoxColumn
        With Fruit
            .HeaderText = "Favs"
            .DataPropertyName = "FruitData"
        End With

        Dim Source As New DataGridViewTextBoxColumn
        With Source
            .HeaderText = "Source"
            .DataPropertyName = "UnderlyingData"
            .Name = "Source"
        End With
        DataGridView1.Columns.AddRange(New DataGridViewColumn() _
                                       {CheckList, Fruit, Source})

        DataGridView1.DataSource = Table
    End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        ' Check = True if value is "A" else unchecked
        Dim Grid As DataGridView = DirectCast(sender, DataGridView)
        If e.ColumnIndex = Grid.FindColumnByName("CheckBoxData").Index Then

            If Grid.Rows.Item(e.RowIndex).Cells(2).Value.ToString() = "A" Then
                Grid.Rows.Item(e.RowIndex).Cells(2).Value = "B"
            Else
                Grid.Rows.Item(e.RowIndex).Cells(2).Value = "A"
            End If
            Grid.EndEdit()
        End If
    End Sub
VS2008 extension used above
    <Runtime.CompilerServices.Extension()> _
    Public Function FindColumnByName(ByVal GridView As DataGridView, _
                                     ByVal ColumnName As String) _
                                     As DataGridViewColumn

        Dim Column As DataGridViewColumn = _
            (From c In GridView _
                .Columns.Cast(Of DataGridViewColumn)() _
                Where c.Name.ToUpper = ColumnName.ToUpper).FirstOrDefault

        If Not IsNothing(Column) Then
            Return Column
        Else
            Throw New Exception(ColumnName & " not found")
        End If
    End Function


KSG
Kevininstructor  Wednesday, May 27, 2009 6:43 PM

Hi Kevininstructor,

"When I click on one of the checkboxes the value in the last column changes but the checkbox checked property only changes after I leave the current row. Same goes for if I change the value in the last column, the checkbox checked value does not change until leaving the row."

It's a by design behavior of DataGridView, even you call EndEdit won't take effect in this situation. DataGridView always commit data to the underline datasource when you leave the editing row. It is done within the DataGridView, so you cannot change it.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Friday, May 29, 2009 4:07 AM
So the code you are posting does not do the update? Or could you be more specific on your question?

(Seriously consider replacing your IIF with the new ternary If statement.)
www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Wednesday, May 27, 2009 11:24 PM
Actually, just found this in the help system:

"For clicks in a DataGridViewCheckBoxCell, this event occurs before the check box changes value, so if you do not want to calculate the expected value based on the current value, you will typically handle the DataGridView..::.CellValueChanged event instead."

Here is the link:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellcontentclick.aspx

Hope this helps.
www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Wednesday, May 27, 2009 11:28 PM
So the code you are posting does not do the update? Or could you be more specific on your question?

(Seriously consider replacing your IIF with the new ternary If statement.)
www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!

Thanks for your reply!

When I click on one of the checkboxes the value in the last column changes but the checkbox checked property only changes after I leave the current row. Same goes for if I change the value in the last column, the checkbox checked value does not change until leaving the row.

What I want is for the checkbox to change when either clicking on the checkbox or changing the value of the last column
KSG
Kevininstructor  Thursday, May 28, 2009 1:05 PM
Actually, just found this in the help system:

"For clicks in a DataGridViewCheckBoxCell, this event occurs before the check box changes value, so if you do not want to calculate the expected value based on the current value, you will typically handle the DataGridView..::.CellValueChanged event instead."

Here is the link:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellcontentclick.aspx

Hope this helps.
www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!

I looked at the docs, tried
        If DataGridView1.IsCurrentCellDirty Then
            DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
        End If
along with
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'DataGridView1.Focus()
        DataGridView1.Rows.Item(DataGridView1.CurrentRow.Index).Cells(2).Value = "A"
        'DataGridView1.InvalidateRow(DataGridView1.CurrentRow.Index)
        DataGridView1.Invalidate()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'DataGridView1.Focus()
        DataGridView1.Rows.Item(DataGridView1.CurrentRow.Index).Cells(2).Value = "B"
        'DataGridView1.InvalidateRow(DataGridView1.CurrentRow.Index)
        DataGridView1.Invalidate()
    End Sub
and get the code to work with invoking either of the two click events two times but never with once click. At least there is some progress but not solved.


KSG
Kevininstructor  Thursday, May 28, 2009 2:11 PM
The help system (from how I was reading) just suggested changing this:

Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
' Check = True if value is "A" else unchecked
Dim Grid As DataGridView = DirectCast(sender, DataGridView)
If e.ColumnIndex = Grid.FindColumnByName("CheckBoxData").Index Then

If Grid.Rows.Item(e.RowIndex).Cells(2).Value.ToString() = "A" Then
Grid.Rows.Item(e.RowIndex).Cells(2).Value = "B"
Else
Grid.Rows.Item(e.RowIndex).Cells(2).Value = "A"
End If
Grid.EndEdit()
End If
End Sub


To use the CellValueChanged event instead of the CellContextClick event. Did that not work for you?
www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Thursday, May 28, 2009 4:06 PM
The help system (from how I was reading) just suggested changing this:

Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
' Check = True if value is "A" else unchecked
Dim Grid As DataGridView = DirectCast(sender, DataGridView)
If e.ColumnIndex = Grid.FindColumnByName("CheckBoxData").Index Then

If Grid.Rows.Item(e.RowIndex).Cells(2).Value.ToString() = "A" Then
Grid.Rows.Item(e.RowIndex).Cells(2).Value = "B"
Else
Grid.Rows.Item(e.RowIndex).Cells(2).Value = "A"
End If
Grid.EndEdit()
End If
End Sub


To use the CellValueChanged event instead of the CellContextClick event. Did that not work for you?
www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!

I read the documentation that you provided a link to and saw that Checkbox columns need to be handled differently so I followed their suggestion to invoke CommitEdit(DataGridViewDataErrorContexts.Commit) followed by called .Invalidate method against the entire DataGridView with the code above in CellValueChanged but the Checkbox still is not updating until leaving the current row.

Also tried the above in "CurrentCellDirtyStateChanged event"

I got some irregular changing of the Checkbox by forcing the data to change in click events of two command buttons
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        DataGridView1.Rows.Item(DataGridView1.CurrentRow.Index).Cells(2).Value = "A"

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        DataGridView1.Rows.Item(DataGridView1.CurrentRow.Index).Cells(2).Value = "B"

    End Sub
If I invoke either button say two or three times I would first see a flash of the checkbox changing on the second execution of the code then on the third time and there after it would seem to work which is simply driving me batty :-[



Any ways thanks again for taking the time to assist me!!!



KSG
Kevininstructor  Thursday, May 28, 2009 8:00 PM

I dug out some old code and found that I used that as well: CurrentCellDirtyStateChanged.

I had to check for which cell's state was changed and if it was the one I wanted, I could make the other columns change to match.

What happened when you tried CurrentCellDirtyStateChanged?


www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Thursday, May 28, 2009 8:27 PM

Hi Kevininstructor,

"When I click on one of the checkboxes the value in the last column changes but the checkbox checked property only changes after I leave the current row. Same goes for if I change the value in the last column, the checkbox checked value does not change until leaving the row."

It's a by design behavior of DataGridView, even you call EndEdit won't take effect in this situation. DataGridView always commit data to the underline datasource when you leave the editing row. It is done within the DataGridView, so you cannot change it.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Friday, May 29, 2009 4:07 AM

Hi Kevininstructor,

"When I click on one of the checkboxes the value in the last column changes but the checkbox checked property only changes after I leave the current row. Same goes for if I change the value in the last column, the checkbox checked value does not change until leaving the row."

It's a by design behavior of DataGridView, even you call EndEdit won't take effect in this situation. DataGridView always commit data to the underline datasource when you leave the editing row. It is done within the DataGridView, so you cannot change it.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.

Thanks for the explaination. I was kind of figuring this was the case that it can not be changed. It would be nice to be able to thou.
KSG
Kevininstructor  Friday, May 29, 2009 1:59 PM

You can use google to search for other answers

Custom Search

More Threads

• datagridview combo box select item event
• How do I binding a windows form control to XML data directly?
• User control with datagridview
• Add New row to DataTable doesn't refresh DataGridView control
• LINQ to XML and DataGridView binding
• CurrentRowIndex not being set with nulls
• Pressing Escape in a DataViewGrid throws an 'System.NullReferenceException'
• datagrid view problem --
• custom datagrid
• Combobox bound to an ArrayList in databound grid